1use crate::fmt;
2use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused};
3use crate::num::NonZero;
4use crate::ops::Try;
5
6/// An iterator that rejects elements while `predicate` returns `true`.
7///
8/// This `struct` is created by the [`skip_while`] method on [`Iterator`]. See its
9/// documentation for more.
10///
11/// [`skip_while`]: Iterator::skip_while
12/// [`Iterator`]: trait.Iterator.html
13#[must_use = "iterators are lazy and do nothing unless consumed"]
14#[stable(feature = "rust1", since = "1.0.0")]
15#[derive(Clone)]
16pub struct SkipWhile<I, P> {
17 iter: I,
18 flag: bool,
19 predicate: P,
20}
21
22impl<I, P> SkipWhile<I, P> {
23 pub(in crate::iter) fn new(iter: I, predicate: P) -> SkipWhile<I, P> {
24 SkipWhile { iter, flag: false, predicate }
25 }
26}
27
28#[stable(feature = "core_impl_debug", since = "1.9.0")]
29impl<I: fmt::Debug, P> fmt::Debug for SkipWhile<I, P> {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 f.debug_struct("SkipWhile").field("iter", &self.iter).field(name:"flag", &self.flag).finish()
32 }
33}
34
35#[stable(feature = "rust1", since = "1.0.0")]
36impl<I: Iterator, P> Iterator for SkipWhile<I, P>
37where
38 P: FnMut(&I::Item) -> bool,
39{
40 type Item = I::Item;
41
42 #[inline]
43 fn next(&mut self) -> Option<I::Item> {
44 fn check<'a, T>(
45 flag: &'a mut bool,
46 pred: &'a mut impl FnMut(&T) -> bool,
47 ) -> impl FnMut(&T) -> bool + 'a {
48 move |x| {
49 if *flag || !pred(x) {
50 *flag = true;
51 true
52 } else {
53 false
54 }
55 }
56 }
57
58 let flag = &mut self.flag;
59 let pred = &mut self.predicate;
60 self.iter.find(check(flag, pred))
61 }
62
63 #[inline]
64 fn size_hint(&self) -> (usize, Option<usize>) {
65 let (_, upper) = self.iter.size_hint();
66 (0, upper) // can't know a lower bound, due to the predicate
67 }
68
69 #[inline]
70 fn try_fold<Acc, Fold, R>(&mut self, mut init: Acc, mut fold: Fold) -> R
71 where
72 Self: Sized,
73 Fold: FnMut(Acc, Self::Item) -> R,
74 R: Try<Output = Acc>,
75 {
76 if !self.flag {
77 match self.next() {
78 Some(v) => init = fold(init, v)?,
79 None => return try { init },
80 }
81 }
82 self.iter.try_fold(init, fold)
83 }
84
85 #[inline]
86 fn fold<Acc, Fold>(mut self, mut init: Acc, mut fold: Fold) -> Acc
87 where
88 Fold: FnMut(Acc, Self::Item) -> Acc,
89 {
90 if !self.flag {
91 match self.next() {
92 Some(v) => init = fold(init, v),
93 None => return init,
94 }
95 }
96 self.iter.fold(init, fold)
97 }
98}
99
100#[stable(feature = "fused", since = "1.26.0")]
101impl<I, P> FusedIterator for SkipWhile<I, P>
102where
103 I: FusedIterator,
104 P: FnMut(&I::Item) -> bool,
105{
106}
107
108#[unstable(issue = "none", feature = "trusted_fused")]
109unsafe impl<I: TrustedFused, P> TrustedFused for SkipWhile<I, P> {}
110
111#[unstable(issue = "none", feature = "inplace_iteration")]
112unsafe impl<P, I> SourceIter for SkipWhile<I, P>
113where
114 I: SourceIter,
115{
116 type Source = I::Source;
117
118 #[inline]
119 unsafe fn as_inner(&mut self) -> &mut I::Source {
120 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
121 unsafe { SourceIter::as_inner(&mut self.iter) }
122 }
123}
124
125#[unstable(issue = "none", feature = "inplace_iteration")]
126unsafe impl<I: InPlaceIterable, F> InPlaceIterable for SkipWhile<I, F> {
127 const EXPAND_BY: Option<NonZero<usize>> = I::EXPAND_BY;
128 const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;
129}
130