resiter_dpc_tmp/
onok.rs

1//
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at http://mozilla.org/MPL/2.0/.
5//
6
7#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
8pub struct OnOk<I, O, E, F>(I, F)
9    where I: Iterator<Item = Result<O, E>>,
10          F: Fn(&O) -> ();
11
12/// Extension trait for `Iterator<Item = Result<T, E>>` to do something on `Ok(_)`
13pub trait OnOkDo<I, O, E, F>
14    where I: Iterator<Item = Result<O, E>>,
15          F: Fn(&O) -> ()
16{
17    fn on_ok(self, F) -> OnOk<I, O, E, F>;
18}
19
20impl<I, O, E, F> OnOkDo<I, O, E, F> for I
21    where I: Iterator<Item = Result<O, E>>,
22          F: Fn(&O) -> ()
23{
24    fn on_ok(self, f: F) -> OnOk<I, O, E, F> {
25        OnOk(self, f)
26    }
27}
28
29impl<I, O, E, F> Iterator for OnOk<I, O, E, F>
30    where I: Iterator<Item = Result<O, E>>,
31          F: Fn(&O) -> ()
32{
33    type Item = Result<O, E>;
34
35    fn next(&mut self) -> Option<Self::Item> {
36        self.0.next().map(|r| r.map(|o| {(self.1)(&o); o }))
37    }
38}
39
40#[test]
41fn test_compile_1() {
42    use std::str::FromStr;
43
44    let _ : Vec<Result<usize, ::std::num::ParseIntError>> = ["1", "2", "3", "4", "5"]
45        .into_iter()
46        .map(|e| usize::from_str(e))
47        .on_ok(|e| println!("Ok: {:?}", e))
48        .collect();
49}
50