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
use std::marker::PhantomData;

use super::*;

struct CollectState<T, E>(Vec<T>, PhantomData<E>);

pub struct CollectResult<I, R> {
    pub items: Vec<I>,
    pub result: R,
}

impl<T, E> ScanFn for CollectState<T, E> {
    type InputItem = T;
    type InputResult = E;
    type OutputItem = ();
    type OutputResult = CollectResult<T, Self::InputResult>;

    fn map_input(mut self, input: Self::InputItem) -> ScanState<Self> {
        self.0.push(input);
        ScanState {
            first: (),
            next: self,
        }
    }

    fn map_result(self, result: Self::InputResult) -> Self::OutputResult {
        CollectResult { items: self.0, result }
    }
}

pub trait Collect
where
    Self: ListFn,
    Self::End: ResultFn,
{
    fn collect(self) -> CollectResult<Self::Item, <Self::End as ResultFn>::Result> {
        self.scan(CollectState(Vec::new(), PhantomData::default()))
            .fold()
            .result()
    }
}

impl<L> Collect for L
where
    Self: ListFn,
    Self::End: ResultFn,
{
}