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
use super::*;

struct CollectState<I: ListFn> {
    result: Vec<I::Item>,
    input: I,
}

impl<I: ListFn> ListFn for CollectState<I> {
    type Item = ();
    type End = Vec<I::Item>;
    fn next(mut self) -> ListState<Self> {
        match self.input.next() {
            ListState::Some(first, next) => {
                self.result.push(first);
                ListState::Some(
                    (),
                    CollectState {
                        result: self.result,
                        input: next,
                    },
                )
            }
            ListState::End(..) => ListState::End(self.result),
        }
    }
}

pub trait Collect: ListFn {
    fn collect(self) -> Vec<Self::Item> {
        CollectState {
            result: Vec::new(),
            input: self,
        }
        .fold()
    }
}

impl<L: ListFn> Collect for L {}