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
54
55
56
57
58
59
use super::*;

pub trait MapResultFn {
    type Input;
    type Output;
    fn map(self, input: Self::Input) -> Self::Output;
}

pub struct MapResultList<I, E>
where
    I: ListFn,
    I::End: ResultFn,
    E: MapResultFn<Input = <I::End as ResultFn>::Result>,
{
    input: I,
    map: E,
}

impl<I, E> ListFn for MapResultList<I, E>
where
    I: ListFn,
    I::End: ResultFn,
    E: MapResultFn<Input = <I::End as ResultFn>::Result>,
{
    type Item = I::Item;
    type End = Id<E::Output>;
    fn next(self) -> ListState<Self> {
        match self.input.next() {
            ListState::Some(some) => ListState::some(
                some.first,
                MapResultList {
                    input: some.next,
                    map: self.map,
                },
            ),
            ListState::End(end) => ListState::End(Id::new(self.map.map(end.result()))),
        }
    }
}

pub trait MapResult
where
    Self: ListFn,
    Self::End: ResultFn,
{
    fn map_result<E: MapResultFn<Input = <Self::End as ResultFn>::Result>>(
        self,
        map: E,
    ) -> MapResultList<Self, E> {
        MapResultList { input: self, map }
    }
}

impl<T> MapResult for T
where
    T: ListFn,
    T::End: ResultFn,
{
}