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
use crate::{Slice, SliceBorrowed, SliceOwned};

macro_rules! map {
    ($owned:ident, $in:ty, $fn:ident) => {
        paste::paste! {
            #[doc = "Maps using a closure on index; see [`" [<Slice $owned>] "::map`]."]
            #[derive(Clone, Copy, Hash)]
            pub struct [<Map $owned>]<S, F>(pub S, pub F);

            impl<S, F, U> Slice for [<Map $owned>]<S, F>
            where
                S: [<Slice $owned>],
                F: Fn($in) -> U,
            {
                type Output = U;

                fn len(&self) -> usize {
                    self.0.len()
                }

                fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
                    Some(f(&self.get_owned(index)?))
                }
            }

            impl<S, F, U> SliceOwned for [<Map $owned>]<S, F>
            where
                S: [<Slice $owned>],
                F: Fn($in) -> U,
            {
                fn get_owned(&self, index: usize) -> Option<U> {
                    self.0.$fn(index).map(&self.1)
                }
            }
        }
    };
}

map!(Owned, S::Output, get_owned);
map!(Borrowed, &S::Output, get);
// map!(MapMut, &mut S::Output, SliceMut, get_mut);