list_fn/
collect.rs

1use core::marker::PhantomData;
2
3use super::*;
4
5struct CollectState<C, E>(C, PhantomData<E>);
6
7impl<C, E> CollectState<C, E> {
8    const fn new(c: C) -> Self {
9        CollectState(c, PhantomData {})
10    }
11}
12
13pub struct CollectResult<C, R> {
14    pub collection: C,
15    pub result: R,
16}
17
18impl<C: Collection, E> ScanFn for CollectState<C, E> {
19    type InputItem = C::Item;
20    type InputResult = E;
21    type OutputItem = ();
22    type OutputResult = CollectResult<C, Self::InputResult>;
23
24    fn map_input(self, input: Self::InputItem) -> ScanState<Self> {
25        ScanState {
26            first: (),
27            next: CollectState::new(self.0.add(input)),
28        }
29    }
30
31    fn map_result(self, result: Self::InputResult) -> Self::OutputResult {
32        CollectResult {
33            collection: self.0,
34            result,
35        }
36    }
37}
38
39pub trait Collect
40where
41    Self: ListFn,
42    Self::End: ResultFn,
43{
44    fn collect<C: Collection<Item = Self::Item>>(
45        self,
46        c: C,
47    ) -> CollectResult<C, <Self::End as ResultFn>::Result> {
48        self.scan(CollectState::new(c)).fold().result()
49    }
50}
51
52impl<L> Collect for L
53where
54    Self: ListFn,
55    Self::End: ResultFn,
56{
57}