Skip to main content

stack/
index_impls.rs

1use std::ops::{Index, IndexMut, Range, RangeFrom, RangeTo, RangeFull};
2use crate::array_vec::ArrayVec;
3use crate::small_vec::{SmallVec, Spilled};
4use coalesce::coalesce;
5use crate::array::Array;
6
7macro_rules! index_impl {
8    ($t:ty) => {
9        impl<T: Array> Index<$t> for ArrayVec<T> where [T::Item]: Index<$t> {
10            type Output = <[T::Item] as Index<$t>>::Output;
11
12            fn index(&self, index: $t) -> &Self::Output {
13                Index::index(&**self, index)
14            }
15        }
16
17        impl<T: Array> IndexMut<$t> for ArrayVec<T> {
18            fn index_mut(&mut self, index: $t) -> &mut Self::Output {
19                IndexMut::index_mut(&mut **self, index)
20            }
21        }
22
23        impl<T: Array, S: Spilled<ArrayVec<T>> + Index<$t>> Index<$t> for SmallVec<T, S> where ArrayVec<T>: Index<$t, Output=<S as Index<$t>>::Output> {
24            type Output = <S as Index<$t>>::Output;
25
26            #[inline]
27            fn index(&self, index: $t) -> &Self::Output {
28                let v = self.0.as_ref();
29                coalesce!(2 => |v| v.index(index))
30            }
31        }
32
33        impl<T: Array, S: Spilled<ArrayVec<T>> + IndexMut<$t>> IndexMut<$t> for SmallVec<T, S> where ArrayVec<T>: IndexMut<$t, Output=<S as Index<$t>>::Output> {
34            #[inline]
35            fn index_mut(&mut self, index: $t) -> &mut Self::Output {
36                let v = self.0.as_mut();
37                coalesce!(2 => |v| v.index_mut(index))
38            }
39        }
40    };
41    ($($t:ty),+) => {
42        $(
43            index_impl!($t);
44        )+
45    };
46}
47
48index_impl!(usize, Range<usize>, RangeFrom<usize>, RangeTo<usize>, RangeFull);