1use std::slice::SliceIndex;
2
3pub trait Indexed<T> {
5 type Element: ?Sized;
7
8 #[must_use]
12 fn get(&self, i: T) -> Option<&Self::Element>;
13}
14pub trait Array {
16 type Element;
18
19 #[must_use]
21 fn iter(&self) -> Box<dyn Iterator<Item = &Self::Element> + '_>;
22
23 #[must_use]
25 fn len(&self) -> usize;
26
27 #[must_use]
29 fn is_empty(&self) -> bool {
30 self.len() == 0
31 }
32}
33
34pub trait IndexedMut<T> {
36 type Element: ?Sized;
38
39 #[must_use]
43 fn get_mut(&mut self, i: T) -> Option<&mut Self::Element>;
44}
45pub trait ArrayMut {
47 type Element;
49
50 #[must_use]
52 fn pop(&mut self) -> Option<Self::Element>;
53
54 fn push(&mut self, e: Self::Element);
56}
57
58impl<T, I> Indexed<I> for Vec<T>
59where
60 I: SliceIndex<[T]>,
61{
62 type Element = <I as SliceIndex<[T]>>::Output;
63 #[inline]
64 fn get(&self, i: I) -> Option<&Self::Element> {
65 <[T]>::get(self, i)
66 }
67}
68
69impl<T> Array for Vec<T> {
70 type Element = T;
71
72 fn iter(&self) -> Box<dyn Iterator<Item = &T> + '_> {
73 Box::new(<[T]>::iter(self))
74 }
75
76 #[inline]
77 fn len(&self) -> usize {
78 self.len()
79 }
80}
81
82impl<T, I> IndexedMut<I> for Vec<T>
83where
84 I: SliceIndex<[T]>,
85{
86 type Element = <I as SliceIndex<[T]>>::Output;
87 #[inline]
88 fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
89 <[T]>::get_mut(self, i)
90 }
91}
92
93impl<T> ArrayMut for Vec<T> {
94 type Element = T;
95
96 #[inline]
97 fn pop(&mut self) -> Option<T> {
98 Vec::pop(self)
99 }
100
101 #[inline]
102 fn push(&mut self, e: T) {
103 Vec::push(self, e);
104 }
105}
106
107#[cfg(feature = "c-abi")]
108mod cabi {
109 use super::{Array, ArrayMut, Indexed, IndexedMut, SliceIndex};
110 use abi_stable::std_types::RVec;
111
112 impl<T, I> Indexed<I> for RVec<T>
113 where
114 I: SliceIndex<[T]>,
115 {
116 type Element = <I as SliceIndex<[T]>>::Output;
117 #[inline]
118 fn get(&self, i: I) -> Option<&Self::Element> {
119 <[T]>::get(self, i)
120 }
121 }
122
123 impl<T> Array for RVec<T> {
124 type Element = T;
125
126 fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &T> + 'i> {
127 Box::new(<[T]>::iter(self))
128 }
129
130 #[inline]
131 fn len(&self) -> usize {
132 self.len()
133 }
134 }
135
136 impl<T, I> IndexedMut<I> for RVec<T>
137 where
138 I: SliceIndex<[T]>,
139 {
140 type Element = <I as SliceIndex<[T]>>::Output;
141 #[inline]
142 fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
143 <[T]>::get_mut(self, i)
144 }
145 }
146
147 #[cfg(feature = "c-abi")]
148 impl<T> ArrayMut for RVec<T> {
149 type Element = T;
150
151 #[inline]
152 fn pop(&mut self) -> Option<T> {
153 abi_stable::std_types::RVec::pop(self)
154 }
155
156 #[inline]
157 fn push(&mut self, e: T) {
158 abi_stable::std_types::RVec::push(self, e);
159 }
160 }
161}