resiter/
oks.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#[cfg(not(test))]
8use core::iter::*;
9#[cfg(test)]
10use std::iter::*;
11
12use util::*;
13
14pub use util::Process as Oks;
15// for backward compatibility with previous implementation
16
17/// Extension trait for `Iterator<Item = Result<T, E>>` to get all `T`s
18#[allow(clippy::type_complexity)]
19pub trait GetOks<T, E>: Sized {
20    /// Iterate over every `Ok` while ignoring every `Err`
21    ///
22    /// ```
23    /// use std::str::FromStr;
24    /// use resiter::oks::GetOks;
25    ///
26    /// let res:Vec<usize> = ["1", "2", "3", "a", "4", "5"]
27    ///     .iter()
28    ///     .map(|e| usize::from_str(e))
29    ///     .oks()
30    ///     .collect();
31    ///
32    /// assert_eq!(
33    ///     res,
34    ///     vec![1,2,3,4,5]
35    /// );
36    /// ```
37    fn oks(self) -> FilterMap<Self, fn(Result<T, E>) -> Option<T>>;
38}
39
40impl<T, E, I> GetOks<T, E> for I
41where
42    I: Iterator<Item = Result<T, E>> + Sized,
43{
44    #[inline]
45    #[allow(clippy::type_complexity)]
46    fn oks(self) -> FilterMap<Self, fn(Result<T, E>) -> Option<T>> {
47        self.filter_map(GetOk::get_ok)
48    }
49}