2 iter::{adapters::SourceIter, FusedIterator, InPlaceIterable},
3 ops::{ControlFlow, Try},
6 /// An iterator that skips over `n` elements of `iter`.
8 /// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
9 /// documentation for more.
11 /// [`skip`]: Iterator::skip
12 /// [`Iterator`]: trait.Iterator.html
13 #[derive(Clone, Debug)]
14 #[must_use = "iterators are lazy and do nothing unless consumed"]
15 #[stable(feature = "rust1", since = "1.0.0")]
22 pub(in crate::iter) fn new(iter: I, n: usize) -> Skip<I> {
27 #[stable(feature = "rust1", since = "1.0.0")]
28 impl<I> Iterator for Skip<I>
32 type Item = <I as Iterator>::Item;
35 fn next(&mut self) -> Option<I::Item> {
46 fn nth(&mut self, n: usize) -> Option<I::Item> {
47 // Can't just add n + self.n due to overflow.
52 self.iter.nth(to_skip - 1)?;
58 fn count(mut self) -> usize {
61 if self.iter.nth(self.n - 1).is_none() {
69 fn last(mut self) -> Option<I::Item> {
72 self.iter.nth(self.n - 1)?;
78 fn size_hint(&self) -> (usize, Option<usize>) {
79 let (lower, upper) = self.iter.size_hint();
81 let lower = lower.saturating_sub(self.n);
82 let upper = match upper {
83 Some(x) => Some(x.saturating_sub(self.n)),
91 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
94 Fold: FnMut(Acc, Self::Item) -> R,
101 if self.iter.nth(n - 1).is_none() {
105 self.iter.try_fold(init, fold)
109 fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
111 Fold: FnMut(Acc, Self::Item) -> Acc,
115 if self.iter.nth(self.n - 1).is_none() {
119 self.iter.fold(init, fold)
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}
126 #[stable(feature = "double_ended_skip_iterator", since = "1.9.0")]
127 impl<I> DoubleEndedIterator for Skip<I>
129 I: DoubleEndedIterator + ExactSizeIterator,
131 fn next_back(&mut self) -> Option<Self::Item> {
132 if self.len() > 0 { self.iter.next_back() } else { None }
136 fn nth_back(&mut self, n: usize) -> Option<I::Item> {
137 let len = self.len();
139 self.iter.nth_back(n)
142 // consume the original iterator
143 self.iter.nth_back(len - 1);
149 fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
152 Fold: FnMut(Acc, Self::Item) -> R,
155 fn check<T, Acc, R: Try<Ok = Acc>>(
157 mut fold: impl FnMut(Acc, T) -> R,
158 ) -> impl FnMut(Acc, T) -> ControlFlow<R, Acc> {
161 let r = fold(acc, x);
162 if n == 0 { ControlFlow::Break(r) } else { ControlFlow::from_try(r) }
167 if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
170 fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
172 Fold: FnMut(Acc, Self::Item) -> Acc,
175 fn ok<Acc, T>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, T) -> Result<Acc, !> {
176 move |acc, x| Ok(f(acc, x))
179 self.try_rfold(init, ok(fold)).unwrap()
183 #[stable(feature = "fused", since = "1.26.0")]
184 impl<I> FusedIterator for Skip<I> where I: FusedIterator {}
186 #[unstable(issue = "none", feature = "inplace_iteration")]
187 unsafe impl<S: Iterator, I: Iterator> SourceIter for Skip<I>
189 I: SourceIter<Source = S>,
194 unsafe fn as_inner(&mut self) -> &mut S {
195 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
196 unsafe { SourceIter::as_inner(&mut self.iter) }
200 #[unstable(issue = "none", feature = "inplace_iteration")]
201 unsafe impl<I: InPlaceIterable> InPlaceIterable for Skip<I> {}