1pub trait ArrayStorage {
3 type T;
5
6 fn insert(&mut self, index: usize, value: Self::T);
8 fn remove(&mut self, index: usize) -> Self::T;
10
11 fn clear(&mut self);
13}
14
15static_assertions::assert_obj_safe!(ArrayStorage<T = ()>);
16
17mod private {
18 pub trait Sealed {}
19}
20
21#[allow(clippy::len_without_is_empty)]
23pub trait ArrayStorageSliceExt: ArrayStorage + private::Sealed {
24 fn len(&self) -> usize
26 where
27 Self: AsRef<[Self::T]>,
28 {
29 self.as_ref().len()
30 }
31
32 fn iter(&self) -> core::slice::Iter<'_, Self::T>
34 where
35 Self: AsRef<[Self::T]>,
36 {
37 self.as_ref().iter()
38 }
39
40 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
52pub trait ConstEmptyArrayStorage: ArrayStorage {
54 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}