starina_inlinedvec/
inlined_vec.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use core::fmt;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ops::RangeBounds;

#[derive(Debug, PartialEq, Eq)]
pub struct TooManyItemsError;

#[derive(Debug, PartialEq, Eq)]
pub struct CapacityError<T>(T);

pub struct InlinedVec<T, const CAP: usize>(arrayvec::ArrayVec<T, CAP>);

impl<T, const CAP: usize> InlinedVec<T, CAP> {
    pub const fn new() -> Self {
        Self(arrayvec::ArrayVec::new_const())
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn pop(&mut self) -> Option<T> {
        self.0.pop()
    }

    pub fn try_push(&mut self, value: T) -> Result<(), CapacityError<T>> {
        self.0
            .try_push(value)
            .map_err(|err| CapacityError(err.element()))
    }

    pub fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), TooManyItemsError>
    where
        T: Copy,
    {
        self.0
            .try_extend_from_slice(other)
            .map_err(|_| TooManyItemsError)
    }

    pub fn as_slice(&self) -> &[T] {
        self.0.as_slice()
    }

    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.0.as_mut_slice()
    }

    pub fn drain<R>(&mut self, range: R) -> arrayvec::Drain<T, CAP>
    where
        R: RangeBounds<usize>,
    {
        self.0.drain(range)
    }
}

impl<T, const CAP: usize> Deref for InlinedVec<T, CAP> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.0.as_slice()
    }
}

impl<T, const CAP: usize> DerefMut for InlinedVec<T, CAP> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.as_mut_slice()
    }
}

impl<T, const CAP: usize> AsRef<[T]> for InlinedVec<T, CAP> {
    fn as_ref(&self) -> &[T] {
        self.0.as_slice()
    }
}

impl<T, const CAP: usize> AsMut<[T]> for InlinedVec<T, CAP> {
    fn as_mut(&mut self) -> &mut [T] {
        self.0.as_mut_slice()
    }
}

impl<T, const CAP: usize> fmt::Debug for InlinedVec<T, CAP>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T, const CAP: usize> Default for InlinedVec<T, CAP> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T, const CAP: usize> IntoIterator for InlinedVec<T, CAP> {
    type Item = T;
    type IntoIter = arrayvec::IntoIter<T, CAP>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a, T, const CAP: usize> IntoIterator for &'a InlinedVec<T, CAP> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<'a, T, const CAP: usize> IntoIterator for &'a mut InlinedVec<T, CAP> {
    type Item = &'a mut T;
    type IntoIter = core::slice::IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}