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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#![no_std]
mod collect;
mod collection;
mod empty;
mod filter;
mod filter_scan;
mod flat_map;
mod flat_scan;
mod flatten;
mod fold;
mod iter;
mod list;
mod map;
mod map_result;
mod option;
mod option_list;
mod result;
mod scan;
mod take;

pub use collect::*;
pub use collection::*;
pub use empty::*;
pub use filter::*;
pub use filter_scan::*;
pub use flat_map::*;
pub use flat_scan::*;
pub use flatten::*;
pub use fold::*;
pub use iter::*;
pub use list::*;
pub use map::*;
pub use map_result::*;
pub use option::*;
pub use option_list::*;
pub use result::*;
pub use scan::*;
pub use take::*;

#[cfg(test)]
mod tests {
    extern crate alloc;
    use alloc::vec::Vec;

    use super::*;

    struct Ref<'a>(&'a mut u32);

    impl<'a> ResultFn for Ref<'a> {
        type Result = ();
        fn result(self) {}
    }

    impl<'a> ListFn for Ref<'a> {
        type Item = u32;
        type End = Self;
        fn next(self) -> ListState<Self> {
            let first = *self.0;
            if first < 10 {
                *self.0 += 1;
                ListState::some(first, self)
            } else {
                ListState::End(self)
            }
        }
    }

    struct Range10(u32);

    impl ResultFn for Range10 {
        type Result = ();
        fn result(self) {}
    }

    impl ListFn for Range10 {
        type Item = u32;
        type End = Self;
        fn next(self) -> ListState<Self> {
            let first = self.0;
            if first < 10 {
                ListState::some(first, Self(first + 1))
            } else {
                ListState::End(self)
            }
        }
    }

    #[test]
    fn as_ref() {
        let mut i = 0;
        let v = Ref(&mut i).iter().collect::<Vec<u32>>();
        assert_eq!(v, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    }

    #[test]
    fn as_range() {
        let v = Range10(0).collect(Vec::default());
        assert_eq!(v.collection, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    }

    #[test]
    fn option() {
        {
            let a = Some(5);
            let v = a.iter().collect::<Vec<u32>>();
            assert_eq!(v, &[5]);
        }
        {
            let a = None;
            let v = a.iter().collect::<Vec<u32>>();
            assert_eq!(v, Vec::new());
        }
    }

    #[test]
    fn foreach() {
        let mut v = Vec::new();
        for elem in Range10(2).iter() {
            v.push(elem);
        }
        assert_eq!(v, &[2, 3, 4, 5, 6, 7, 8, 9])
    }
}