value_trait/
array.rs

1use std::slice::SliceIndex;
2
3/// Trait for indexing into an array
4pub trait Indexed<T> {
5    /// Elements of the array
6    type Element: ?Sized;
7
8    /// Gets a ref to a value based on n index, returns `None` if the
9    /// current Value isn't an Array or doesn't contain the index
10    /// it was asked for.
11    #[must_use]
12    fn get(&self, i: T) -> Option<&Self::Element>;
13}
14/// A trait for the minimal common functionality of a vale array
15pub trait Array {
16    /// Elements of the array
17    type Element;
18
19    /// Iterates over the values paris
20    #[must_use]
21    fn iter(&self) -> Box<dyn Iterator<Item = &Self::Element> + '_>;
22
23    /// Number of key/value pairs
24    #[must_use]
25    fn len(&self) -> usize;
26
27    /// Returns if the array is empty
28    #[must_use]
29    fn is_empty(&self) -> bool {
30        self.len() == 0
31    }
32}
33
34/// Trait for indexing into an array
35pub trait IndexedMut<T> {
36    /// Elements of the array
37    type Element: ?Sized;
38
39    /// Gets a ref to a value based on n index, returns `None` if the
40    /// current Value isn't an Array or doesn't contain the index
41    /// it was asked for.
42    #[must_use]
43    fn get_mut(&mut self, i: T) -> Option<&mut Self::Element>;
44}
45/// Mutability functions for a value array
46pub trait ArrayMut {
47    /// Elements of the array
48    type Element;
49
50    /// Returns the last element of the array or `None`
51    #[must_use]
52    fn pop(&mut self) -> Option<Self::Element>;
53
54    /// Appends e to the end of the `Array`
55    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}