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
52
53
use core::ops::{Index, IndexMut};

use crate::{Chain, Cycle, Interleave, Reverse, Slice, SliceMut, SliceOf, SliceOfMut};

macro_rules! impl_index {
    ($(
        $(mut $($is_mut:lifetime)?)? $typ:ident [$($lifetimes:lifetime),* $($generics:ident),*] = $bound:ident
    ;)*) =>{$(
        impl<
            $($lifetimes,)* T, $($generics,)*
        > Index<usize> for $typ<$($lifetimes,)* T, $($generics,)*>
        where
            $($generics : $bound<T>,)*
        {
            type Output = T;

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

        impl<
            $($lifetimes,)* T, $($generics,)*
        > IndexMut<usize> for $typ<$($lifetimes,)* T, $($generics,)*>
        where
            $typ<T, $($generics,)*>: SliceMut<T>,
            $($generics : SliceMut<T>,)*
        {
            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 was {index}",
                    )
                })
            }
        }
    )*};
}

impl_index! {
    Chain[A, B] = Slice;
    Cycle[A] = Slice;
    Interleave[A, B] = Slice;
    Reverse[A] = Slice;
    SliceOf[A] = Slice;
    SliceOfMut[A] = SliceMut;
}