Skip to main content

ts_array256/
storage.rs

1/// Backing storage for [`Array256`][crate::Array256].
2pub trait ArrayStorage {
3    /// The value contained in the storage.
4    type T;
5
6    /// Insert a value into the storage at `index`.
7    fn insert(&mut self, index: usize, value: Self::T);
8    /// Remove the value from the storage at `index`.
9    fn remove(&mut self, index: usize) -> Self::T;
10
11    /// Empty the whole storage.
12    fn clear(&mut self);
13}
14
15static_assertions::assert_obj_safe!(ArrayStorage<T = ()>);
16
17mod private {
18    pub trait Sealed {}
19}
20
21/// Extension methods for [`ArrayStorage`].
22#[allow(clippy::len_without_is_empty)]
23pub trait ArrayStorageSliceExt: ArrayStorage + private::Sealed {
24    /// Length of the storage.
25    fn len(&self) -> usize
26    where
27        Self: AsRef<[Self::T]>,
28    {
29        self.as_ref().len()
30    }
31
32    /// Iterate mutable references to elements in the storage.
33    fn iter(&self) -> core::slice::Iter<'_, Self::T>
34    where
35        Self: AsRef<[Self::T]>,
36    {
37        self.as_ref().iter()
38    }
39
40    /// Iterate mutable references to elements in the storage.
41    fn iter_mut(&mut self) -> core::slice::IterMut<'_, Self::T>
42    where
43        Self: AsMut<[Self::T]>,
44    {
45        self.as_mut().iter_mut()
46    }
47}
48
49impl<T> private::Sealed for T where T: ArrayStorage {}
50impl<T> ArrayStorageSliceExt for T where T: ArrayStorage + private::Sealed {}
51
52/// Const-construct an `ArrayStorage`.
53pub trait ConstEmptyArrayStorage: ArrayStorage {
54    /// The empty value for this storage.
55    const EMPTY: Self;
56}
57
58impl<T> ArrayStorage for alloc::vec::Vec<T> {
59    type T = T;
60
61    fn insert(&mut self, index: usize, value: Self::T) {
62        self.insert(index, value)
63    }
64
65    fn remove(&mut self, index: usize) -> Self::T {
66        self.remove(index)
67    }
68
69    fn clear(&mut self) {
70        self.clear()
71    }
72}
73
74impl<T> ConstEmptyArrayStorage for alloc::vec::Vec<T> {
75    const EMPTY: Self = alloc::vec::Vec::new();
76}
77
78impl<T, LenT, S> ArrayStorage for heapless::vec::VecInner<T, LenT, S>
79where
80    LenT: heapless::LenType,
81    S: heapless::vec::VecStorage<T>,
82{
83    type T = T;
84
85    #[inline]
86    fn insert(&mut self, index: usize, value: Self::T) {
87        self.insert(index, value)
88            .map_err(|_| ())
89            .expect("heapless::Vec was too small to act as ArrayStorage");
90    }
91
92    #[inline]
93    fn remove(&mut self, index: usize) -> Self::T {
94        self.remove(index)
95    }
96
97    fn clear(&mut self) {
98        self.clear();
99    }
100}
101
102impl<const N: usize, T, LenT> ConstEmptyArrayStorage for heapless::Vec<T, N, LenT>
103where
104    LenT: heapless::LenType,
105{
106    const EMPTY: Self = heapless::Vec::new();
107}
108
109#[cfg(feature = "smallvec")]
110impl<A> ArrayStorage for smallvec::SmallVec<A>
111where
112    A: smallvec::Array,
113{
114    type T = A::Item;
115
116    fn insert(&mut self, index: usize, value: Self::T) {
117        self.insert(index, value)
118    }
119
120    fn remove(&mut self, index: usize) -> Self::T {
121        self.remove(index)
122    }
123
124    fn clear(&mut self) {
125        self.clear()
126    }
127}
128
129#[cfg(feature = "smallvec")]
130impl<T, const N: usize> ConstEmptyArrayStorage for smallvec::SmallVec<[T; N]> {
131    const EMPTY: Self = smallvec::SmallVec::new_const();
132}