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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
//
// 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 filter one kind of result (and leaving the other as is)
pub trait Filter<O, E>: Sized {
/// Filter `Ok` items while keeping `Err`
///
/// ```
/// use resiter::filter::Filter;
/// use std::str::FromStr;
///
/// let mapped: Vec<_> = ["1", "2", "a", "4", "5"]
/// .iter()
/// .map(|txt| usize::from_str(txt))
/// .filter_ok(|i| i % 2 == 0)
/// .collect();
/// assert_eq!(mapped.len(), 3);
/// assert_eq!(mapped[0], Ok(2));
/// assert!(mapped[1].is_err());
/// assert_eq!(mapped[2], Ok(4))
/// ```
fn filter_ok<F>(self, _: F) -> FilterOk<Self, F>
where
F: FnMut(&O) -> bool;
/// Filter `Err` values while keeping `Ok`
///
/// ```
/// use resiter::filter::Filter;
/// use std::str::FromStr;
///
/// let mapped: Vec<_> = ["1", "2", "a", "4", "5"]
/// .iter()
/// .map(|txt| usize::from_str(txt))
/// .filter_err(|_| false)
/// .collect();
///
/// assert_eq!(mapped, vec![Ok(1), Ok(2), Ok(4), Ok(5)]);
/// ```
fn filter_err<F>(self, _: F) -> FilterErr<Self, F>
where
F: FnMut(&E) -> bool;
}
impl<I, O, E> Filter<O, E> for I
where
I: Iterator<Item = Result<O, E>> + Sized,
{
#[inline]
fn filter_ok<F>(self, f: F) -> FilterOk<Self, F>
where
F: FnMut(&O) -> bool,
{
FilterOk { iter: self, f }
}
#[inline]
fn filter_err<F>(self, f: F) -> FilterErr<Self, F>
where
F: FnMut(&E) -> bool,
{
FilterErr { iter: self, f }
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FilterOk<I, F> {
iter: I,
f: F,
}
impl<I, O, E, F> Iterator for FilterOk<I, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&O) -> bool,
{
type Item = Result<O, E>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Ok(x)) => {
if (self.f)(&x) {
return Some(Ok(x));
}
}
other => {
return other;
}
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let hint_sup = self.iter.size_hint().1;
(0, hint_sup)
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FilterErr<I, F> {
iter: I,
f: F,
}
impl<I, O, E, F> Iterator for FilterErr<I, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(&E) -> bool,
{
type Item = Result<O, E>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Err(x)) => {
if (self.f)(&x) {
return Some(Err(x));
}
}
other => {
return other;
}
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let hint_sup = self.iter.size_hint().1;
(0, hint_sup)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_ok_hint() {
use std::str::FromStr;
let hint = ["1", "2", "a", "4", "5"]
.iter()
.map(|txt| usize::from_str(txt))
.filter_ok(|i| i % 2 == 0)
.size_hint();
assert_eq!(hint, (0, Some(5)));
}
#[test]
fn test_filter_err_hint() {
use std::str::FromStr;
let hint = ["1", "2", "a", "4", "5"]
.iter()
.map(|txt| usize::from_str(txt))
.filter_err(|_| false)
.size_hint();
assert_eq!(hint, (0, Some(5)));
}
}