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
use crate::internal_iter::FromInternalIterator;
pub trait InternalIterator {
type Item;
fn for_each<F>(self, f: &mut F) -> bool
where
F: FnMut(Self::Item) -> bool;
fn collect<F>(self) -> F
where
Self: Sized,
F: FromInternalIterator<Self::Item>,
{
FromInternalIterator::from_internal_iter(self)
}
fn map<R, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Item) -> R,
{
Map {
internal_iter: self,
f,
}
}
}
impl<I> InternalIterator for I
where
I: Iterator,
{
type Item = I::Item;
fn for_each<F>(self, f: &mut F) -> bool
where
F: FnMut(Self::Item) -> bool,
{
for item in self {
if !f(item) {
return false;
}
}
true
}
}
#[derive(Clone, Debug)]
pub struct Map<I, F> {
internal_iter: I,
f: F,
}
impl<R, I, F> InternalIterator for Map<I, F>
where
I: InternalIterator,
F: FnMut(I::Item) -> R,
{
type Item = R;
fn for_each<G>(mut self, g: &mut G) -> bool
where
G: FnMut(Self::Item) -> bool,
{
self.internal_iter.for_each({
let f = &mut self.f;
&mut move |item| g((f)(item))
})
}
}