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
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct OnOk<I, O, E, F>(I, F)
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&O);
/// Extension trait for `Iterator<Item = Result<T, E>>` to do something on `Ok(_)`
pub trait OnOkDo<I, O, E, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&O),
{
/// Perform a side effect on each Ok value
///
/// ```
/// use resiter::onok::OnOkDo;
/// use std::str::FromStr;
///
/// let mut oks = Vec::new();
/// let _: Vec<Result<usize, ::std::num::ParseIntError>> = ["1", "2", "a", "b", "5"]
/// .iter()
/// .map(|e| usize::from_str(e))
/// .on_ok(|e| oks.push(e.to_owned()))
/// .collect();
///
/// assert_eq!(oks, vec![1, 2, 5]);
/// ```
fn on_ok(self, _: F) -> OnOk<I, O, E, F>;
}
impl<I, O, E, F> OnOkDo<I, O, E, F> for I
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&O),
{
#[inline]
fn on_ok(self, f: F) -> OnOk<I, O, E, F> {
OnOk(self, f)
}
}
impl<I, O, E, F> Iterator for OnOk<I, O, E, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&O),
{
type Item = Result<O, E>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|r| {
r.map(|o| {
(self.1)(&o);
o
})
})
}
}