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 CollectVec<I: ListFn> {
    end: Vec<I::Item>,
    input: I,
}

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

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

impl<L: ListFn> Collect for L {}