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
use crate::{
and_then::AndThen, map_err::MapErr, try_filter::TryFilter, try_filter_map::TryFilterMap,
try_flat_map::TryFlatMap, try_flat_map_results::TryFlatMapResults, try_flatten::TryFlatten,
try_flatten_results::TryFlattenResults, try_scan::TryScan,
};
pub trait IteratorExt {
fn try_scan<St, F, T, U, E>(self, init: St, f: F) -> TryScan<Self, St, F>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(&mut St, T) -> Result<Option<U>, E>,
{
TryScan {
iter: Some(self),
state: init,
f,
}
}
fn try_flat_map<F, T, U, V, E>(self, f: F) -> TryFlatMap<Self, F, U::IntoIter>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(T) -> Result<U, E>,
U: IntoIterator<Item = V>,
{
TryFlatMap {
iter: Some(self),
sub_iter: None,
f,
}
}
fn try_flatten<T, U, E>(self) -> TryFlatten<Self, T::IntoIter>
where
Self: Sized + Iterator<Item = Result<T, E>>,
T: IntoIterator<Item = U>,
{
TryFlatten {
iter: Some(self),
sub_iter: None,
}
}
fn try_flat_map_results<F, T, U, V, E>(self, f: F) -> TryFlatMapResults<Self, F, U::IntoIter>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(T) -> Result<U, E>,
U: IntoIterator<Item = Result<V, E>>,
{
TryFlatMapResults {
iter: Some(self),
sub_iter: None,
f,
}
}
fn try_flatten_results<T, U, E>(self) -> TryFlattenResults<Self, T::IntoIter>
where
Self: Sized + Iterator<Item = Result<T, E>>,
T: IntoIterator<Item = Result<U, E>>,
{
TryFlattenResults {
iter: Some(self),
sub_iter: None,
}
}
fn try_filter<F, T, E>(self, f: F) -> TryFilter<Self, F>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(&T) -> Result<bool, E>,
{
TryFilter {
iter: Some(self),
f,
}
}
fn try_filter_map<F, T, U, E>(self, f: F) -> TryFilterMap<Self, F>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(T) -> Result<Option<U>, E>,
{
TryFilterMap {
iter: Some(self),
f,
}
}
fn map_err<F, Ein, Eout>(self, f: F) -> MapErr<Self, F>
where
Self: Sized,
F: FnMut(Ein) -> Eout,
{
MapErr { iter: self, f }
}
fn and_then<F, T, U, E>(self, f: F) -> AndThen<Self, F>
where
Self: Sized + Iterator<Item = Result<T, E>>,
F: FnMut(T) -> Result<U, E>,
{
AndThen {
iter: Some(self),
f,
}
}
}
impl<I> IteratorExt for I where I: Iterator {}