use crate::fmt; use crate::iter::{adapters::SourceIter, InPlaceIterable}; use crate::ops::{ControlFlow, Try}; /// An iterator that only accepts elements while `predicate` returns `Some(_)`. /// /// This `struct` is created by the [`map_while`] method on [`Iterator`]. See its /// documentation for more. /// /// [`map_while`]: Iterator::map_while /// [`Iterator`]: trait.Iterator.html #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iter_map_while", since = "1.57.0")] #[derive(Clone)] pub struct MapWhile { iter: I, predicate: P, } impl MapWhile { pub(in crate::iter) fn new(iter: I, predicate: P) -> MapWhile { MapWhile { iter, predicate } } } #[stable(feature = "iter_map_while", since = "1.57.0")] impl fmt::Debug for MapWhile { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MapWhile").field("iter", &self.iter).finish() } } #[stable(feature = "iter_map_while", since = "1.57.0")] impl Iterator for MapWhile where P: FnMut(I::Item) -> Option, { type Item = B; #[inline] fn next(&mut self) -> Option { let x = self.iter.next()?; (self.predicate)(x) } #[inline] fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } #[inline] fn try_fold(&mut self, init: Acc, mut fold: Fold) -> R where Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try, { let Self { iter, predicate } = self; iter.try_fold(init, |acc, x| match predicate(x) { Some(item) => ControlFlow::from_try(fold(acc, item)), None => ControlFlow::Break(try { acc }), }) .into_try() } #[inline] fn fold(mut self, init: Acc, fold: Fold) -> Acc where Self: Sized, Fold: FnMut(Acc, Self::Item) -> Acc, { #[inline] fn ok(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result { move |acc, x| Ok(f(acc, x)) } self.try_fold(init, ok(fold)).unwrap() } } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl SourceIter for MapWhile where I: SourceIter, { type Source = I::Source; #[inline] unsafe fn as_inner(&mut self) -> &mut I::Source { // SAFETY: unsafe function forwarding to unsafe function with the same requirements unsafe { SourceIter::as_inner(&mut self.iter) } } } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for MapWhile where P: FnMut(I::Item) -> Option { }