1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use core::ops::{Index, IndexMut};

use crate::{
    Chain, Cycle, Interleave, Reverse, Slice, SliceBorrowed, SliceMut, SliceOf, SplitMut, Unique,
};

macro_rules! impl_index {
    ($(
        $typ:ident [$($lt:lifetime),* $($generics:ident),*] $(= $unique:ident)?
    ;)*) => {$(
        impl<
            $($lt,)* T, S $(, $generics)*,
        > Index<usize> for $typ<$($lt,)* S $(, $generics)*>
        where
            S: SliceBorrowed<Output = T>,
            $( $generics: SliceBorrowed<Output = T>,)*
        {
            type Output = <Self as Slice>::Output;

            fn index(&self, index: usize) -> &Self::Output {
                self.get(index).unwrap_or_else(|| {
                    panic!("index out of bounds: the len is {} but the index is {index}", self.len())
                })
            }
        }

        impl<
            $($lt,)* T, S $(, $generics)*,
        > IndexMut<usize> for $typ<$($lt,)* S $(, $generics)*>
        where
            S: SliceBorrowed<Output = T> + SliceMut $(+ $unique)?,
            $( $generics: SliceBorrowed<Output = T> + SliceMut,)*
        {
            fn index_mut(&mut self, index: usize) -> &mut Self::Output {
                let len = self.len();
                self.get_mut(index).unwrap_or_else(|| {
                    panic!("index out of bounds: the len is {len} but the index is {index}")
                })
            }
        }
    )*};
}

impl_index! {
    Chain[S2];
    Cycle[];
    Interleave[S2];
    Reverse[];
    SliceOf[];
    SplitMut['a] = Unique;
}