//! “Lifted” from itertools pub struct UntilDone<'a, I, E: 'a> { error: &'a mut Result<(), E>, iter: I, } impl<'a, I, T, E> Iterator for UntilDone<'a, I, E> where I: Iterator>, { type Item = T; fn next(&mut self) -> Option { match self.iter.next() { Some(Ok(x)) => Some(x), Some(Err(e)) => { *self.error = Err(e); None } None => None, } } } pub trait Until { /// “Lift” a function of the values of an iterator so that it can process /// an iterator of `Result` values instead. fn until_done(self, f: impl FnOnce(UntilDone) -> R) -> Result; } impl> Until for I { fn until_done(self, f: impl FnOnce(UntilDone) -> R) -> Result { let mut error = Ok(()); let result = f(UntilDone { error: &mut error, iter: self, }); error.map(|_| result) } }