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

pub struct TakeList<L: ListFn> {
    next: L,
    count: usize,
}

impl<L: ListFn> ListFn for TakeList<L> {
    type Item = L::Item;
    type End = ();
    fn next(self) -> ListState<Self> {
        if self.count == 0 {
            ListState::End(())
        } else {
            match self.next.next() {
                ListState::Some(some) => ListState::some(
                    some.first,
                    TakeList {
                        next: some.next,
                        count: self.count - 1,
                    },
                ),
                ListState::End(_) => ListState::End(()),
            }
        }
    }
}

pub trait Take: ListFn {
    fn take(self, count: usize) -> TakeList<Self> {
        TakeList { next: self, count }
    }
}

impl<L: ListFn> Take for L {}