resiter_dpc_tmp/
util.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
7pub trait GetErr<T> {
8    fn get_err(self) -> Option<T>;
9}
10
11impl<U, T> GetErr<T> for Result<U, T> {
12    fn get_err(self) -> Option<T> {
13        self.err()
14    }
15}
16
17pub trait GetOk<T> {
18    fn get_ok(self) -> Option<T>;
19}
20
21impl<T, E> GetOk<T> for Result<T, E> {
22    fn get_ok(self) -> Option<T> {
23        self.ok()
24    }
25}
26
27
28/// Extend any Iterator with a `process` method, equivalent to a fallible for_each.
29pub trait Process<T> {
30    fn process<R: Default, E, F>(self, f: F) -> Result<R, E>
31        where F: Fn(T) -> Result<R, E>;
32}
33
34impl<I: Iterator> Process<I::Item> for I {
35    /// Process all errors with a lambda
36    fn process<R: Default, E, F>(self, f: F) -> Result<R, E>
37        where F: Fn(I::Item) -> Result<R, E>
38    {
39        for element in self {
40            let _ = f(element)?;
41        }
42        Ok(R::default())
43    }
44}