1#![no_std]
2mod collect;
3mod collection;
4mod empty;
5mod filter;
6mod filter_scan;
7mod flat_map;
8mod flat_scan;
9mod flatten;
10mod fold;
11mod iter;
12mod list;
13mod map;
14mod map_result;
15mod option;
16mod option_list;
17mod result;
18mod scan;
19mod take;
20
21pub use collect::*;
22pub use collection::*;
23pub use empty::*;
24pub use filter::*;
25pub use filter_scan::*;
26pub use flat_map::*;
27pub use flat_scan::*;
28pub use flatten::*;
29pub use fold::*;
30pub use iter::*;
31pub use list::*;
32pub use map::*;
33pub use map_result::*;
34pub use option::*;
35pub use option_list::*;
36pub use result::*;
37pub use scan::*;
38pub use take::*;
39
40#[cfg(test)]
41mod tests {
42 extern crate alloc;
43 use alloc::vec::Vec;
44
45 use super::*;
46
47 struct Ref<'a>(&'a mut u32);
48
49 impl<'a> ResultFn for Ref<'a> {
50 type Result = ();
51 fn result(self) {}
52 }
53
54 impl<'a> ListFn for Ref<'a> {
55 type Item = u32;
56 type End = Self;
57 fn next(self) -> ListState<Self> {
58 let first = *self.0;
59 if first < 10 {
60 *self.0 += 1;
61 ListState::some(first, self)
62 } else {
63 ListState::End(self)
64 }
65 }
66 }
67
68 struct Range10(u32);
69
70 impl ResultFn for Range10 {
71 type Result = ();
72 fn result(self) {}
73 }
74
75 impl ListFn for Range10 {
76 type Item = u32;
77 type End = Self;
78 fn next(self) -> ListState<Self> {
79 let first = self.0;
80 if first < 10 {
81 ListState::some(first, Self(first + 1))
82 } else {
83 ListState::End(self)
84 }
85 }
86 }
87
88 #[test]
89 fn as_ref() {
90 let mut i = 0;
91 let v = Ref(&mut i).iter().collect::<Vec<u32>>();
92 assert_eq!(v, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
93 }
94
95 #[test]
96 fn as_range() {
97 let v = Range10(0).collect(Vec::default());
98 assert_eq!(v.collection, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
99 }
100
101 #[test]
102 fn option() {
103 {
104 let a = Some(5);
105 let v = a.iter().collect::<Vec<u32>>();
106 assert_eq!(v, &[5]);
107 }
108 {
109 let a = None;
110 let v = a.iter().collect::<Vec<u32>>();
111 assert_eq!(v, Vec::new());
112 }
113 }
114
115 #[test]
116 fn foreach() {
117 let mut v = Vec::new();
118 for elem in Range10(2).iter() {
119 v.push(elem);
120 }
121 assert_eq!(v, &[2, 3, 4, 5, 6, 7, 8, 9])
122 }
123}