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
//
// 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/.
//

/// Extension trait for `Iterator<Item = Result<O, E>>` to iter until an error is encountered.
pub trait WhileOk<O, E>
{
    fn while_ok<F>(self, F) -> Result<(), E>
        where F: FnMut(O) -> ();
}

impl<I, O, E> WhileOk<O, E> for I
    where I: Iterator<Item = Result<O, E>>,
{
    fn while_ok<F>(self, mut f: F) -> Result<(), E>
        where F: FnMut(O) -> ()
    {
        for res in self {
            f(res?);
        }
        Ok(())
    }
}



#[test]
fn test_while_ok_ok() {
    use std::str::FromStr;

    let mut s = 0;

    let res = ["1", "2", "3", "4", "5"]
        .into_iter()
        .map(|txt| usize::from_str(txt))
        .while_ok(|i| s += i);

    assert_eq!(s, 15);
    assert!(res.is_ok());
}

#[test]
fn test_while_ok_err() {
    use std::str::FromStr;

    let mut s = 0;

    let res = ["1", "2", "a", "4", "5"]
        .into_iter()
        .map(|txt| usize::from_str(txt))
        .while_ok(|i| s += i);

    assert_eq!(s, 3);
    assert!(res.is_err());
}