#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct UnwrapWith<I, O, E, F>(I, F)
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(E) -> Option<O>;
impl<I, O, E, F> Iterator for UnwrapWith<I, O, E, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(E) -> Option<O>,
{
type Item = O;
fn next(&mut self) -> Option<Self::Item> {
while let Some(o) = self.0.by_ref().next() {
match o {
Ok(t) => return Some(t),
Err(e) => {
if let Some(t) = (self.1)(e) {
return Some(t);
}
}
}
}
None
}
}
pub trait UnwrapWithExt<I, O, E, F>
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(E) -> Option<O>,
{
fn unwrap_with(self, _: F) -> UnwrapWith<I, O, E, F>;
}
impl<I, O, E, F> UnwrapWithExt<I, O, E, F> for I
where
I: Iterator<Item = Result<O, E>>,
F: FnMut(E) -> Option<O>,
{
#[inline]
fn unwrap_with(self, f: F) -> UnwrapWith<I, O, E, F> {
UnwrapWith(self, f)
}
}