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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use super::*;
use take_mut::take;

/// Any `&mut Iterator` is a `ListFn<End = Self>`.
impl<I: Iterator> ListFn for &mut I {
    type Item = I::Item;
    type End = Self;
    /// Converts an iterator into a list.
    fn next(self) -> ListState<Self> {
        match self.next() {
            Option::None => ListState::End(self),
            Option::Some(first) => ListState::some(first, self),
        }
    }
}

impl<I: Iterator> ResultFn for &mut I {
    type Result = ();
    fn result(self) {}
}

pub struct ListIterator<S: ListFn<End = S>>(S);

impl<S: ListFn<End = S>> Iterator for ListIterator<S> {
    type Item = S::Item;
    fn next(&mut self) -> Option<Self::Item> {
        let mut result = None;
        take(&mut self.0, |list| match list.next() {
            ListState::End(end) => end,
            ListState::Some(some) => {
                result = Some(some.first);
                some.next
            }
        });
        result
    }
}

/// Note: we can't use the standard std::iter::IntoIterator because it has
/// a conflicting implementation.
pub trait Iter: ListFn<End = Self> {
    /// Converts a list to an iterator.
    fn iter(self) -> ListIterator<Self> {
        ListIterator(self)
    }
}

impl<S: ListFn<End = Self>> Iter for S {}

pub enum ListIteratorWrap<L: ListFn> {
    List(L),
    End(L::End),
}

impl<L: ListFn> Iterator for ListIteratorWrap<L> {
    type Item = L::Item;
    fn next(&mut self) -> Option<Self::Item> {
        let mut result = None;
        take(self, |wrap| match wrap {
            ListIteratorWrap::List(list) => match list.next() {
                ListState::End(end) => ListIteratorWrap::End(end),
                ListState::Some(some) => {
                    result = Some(some.first);
                    ListIteratorWrap::List(some.next)
                }
            },
            end => end,
        });
        result
    }
}

pub trait IterWrap: ListFn {
    fn iter_wrap(self) -> ListIteratorWrap<Self> {
        ListIteratorWrap::List(self)
    }
}

impl<L: ListFn> IterWrap for L {}