1use core::ops::{Index, IndexMut};
2
3use crate::{
4 Chain, Cycle, Interleave, Reverse, Slice, SliceBorrowed, SliceMut, SliceOf, SplitMut, Unique,
5};
6
7macro_rules! impl_index {
8 ($(
9 $typ:ident [$($lt:lifetime),* $($generics:ident),*] $(= $unique:ident)?
10 ;)*) => {$(
11 impl<
12 $($lt,)* T, S $(, $generics)*,
13 > Index<usize> for $typ<$($lt,)* S $(, $generics)*>
14 where
15 S: SliceBorrowed<Output = T>,
16 $( $generics: SliceBorrowed<Output = T>,)*
17 {
18 type Output = <Self as Slice>::Output;
19
20 fn index(&self, index: usize) -> &Self::Output {
21 self.get(index).unwrap_or_else(|| {
22 panic!("index out of bounds: the len is {} but the index is {index}", self.len())
23 })
24 }
25 }
26
27 impl<
28 $($lt,)* T, S $(, $generics)*,
29 > IndexMut<usize> for $typ<$($lt,)* S $(, $generics)*>
30 where
31 S: SliceBorrowed<Output = T> + SliceMut $(+ $unique)?,
32 $( $generics: SliceBorrowed<Output = T> + SliceMut,)*
33 {
34 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
35 let len = self.len();
36 self.get_mut(index).unwrap_or_else(|| {
37 panic!("index out of bounds: the len is {len} but the index is {index}")
38 })
39 }
40 }
41 )*};
42}
43
44impl_index! {
45 Chain[S2];
46 Cycle[];
47 Interleave[S2];
48 Reverse[];
49 SliceOf[];
50 SplitMut['a] = Unique;
51}