pub use fallible_iterator::FallibleIterator;
pub trait FallibleIteratorMut {
type Item;
type Error;
fn next(&mut self) -> Result<Option<&mut Self::Item>, Self::Error>;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
#[inline]
fn map<F, B>(&mut self, f: F) -> Map<'_, Self, F>
where
Self: Sized,
F: FnMut(&mut Self::Item) -> Result<B, Self::Error>,
{
Map { it: self, f }
}
}
pub struct Map<'a, I, F> {
it: &'a mut I,
f: F,
}
impl<'a, I, F, B> FallibleIterator for Map<'a, I, F>
where
I: FallibleIteratorMut,
F: FnMut(&mut I::Item) -> Result<B, I::Error>,
{
type Item = B;
type Error = I::Error;
#[inline]
fn next(&mut self) -> Result<Option<B>, I::Error> {
match self.it.next() {
Ok(Some(v)) => Ok(Some((self.f)(v)?)),
Ok(None) => Ok(None),
Err(e) => Err(e),
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
}