resiter/
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/// Extend any Iterator with a `process` method, equivalent to a fallible for_each.
28pub trait Process<T> {
29    fn process<R: Default, E, F>(self, f: F) -> Result<R, E>
30    where
31        F: Fn(T) -> Result<R, E>;
32}
33
34impl<I: Iterator> Process<I::Item> for I {
35    /// Process all errors with a lambda
36    #[inline]
37    fn process<R: Default, E, F>(self, f: F) -> Result<R, E>
38    where
39        F: Fn(I::Item) -> Result<R, E>,
40    {
41        for element in self {
42            let _ = f(element)?;
43        }
44        Ok(R::default())
45    }
46}