1 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
2 use crate::ops::{ControlFlow, Try};
4 /// An iterator that skips over `n` elements of `iter`.
6 /// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
7 /// documentation for more.
9 /// [`skip`]: Iterator::skip
10 /// [`Iterator`]: trait.Iterator.html
11 #[derive(Clone, Debug)]
12 #[must_use = "iterators are lazy and do nothing unless consumed"]
13 #[stable(feature = "rust1", since = "1.0.0")]
20 pub(in crate::iter) fn new(iter: I, n: usize) -> Skip<I> {
25 #[stable(feature = "rust1", since = "1.0.0")]
26 impl<I> Iterator for Skip<I>
30 type Item = <I as Iterator>::Item;
33 fn next(&mut self) -> Option<I::Item> {
44 fn nth(&mut self, n: usize) -> Option<I::Item> {
45 // Can't just add n + self.n due to overflow.
50 self.iter.nth(to_skip - 1)?;
56 fn count(mut self) -> usize {
59 if self.iter.nth(self.n - 1).is_none() {
67 fn last(mut self) -> Option<I::Item> {
70 self.iter.nth(self.n - 1)?;
76 fn size_hint(&self) -> (usize, Option<usize>) {
77 let (lower, upper) = self.iter.size_hint();
79 let lower = lower.saturating_sub(self.n);
80 let upper = match upper {
81 Some(x) => Some(x.saturating_sub(self.n)),
89 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
92 Fold: FnMut(Acc, Self::Item) -> R,
99 if self.iter.nth(n - 1).is_none() {
103 self.iter.try_fold(init, fold)
107 fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
109 Fold: FnMut(Acc, Self::Item) -> Acc,
113 if self.iter.nth(self.n - 1).is_none() {
117 self.iter.fold(init, fold)
121 #[stable(feature = "rust1", since = "1.0.0")]
122 impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}
124 #[stable(feature = "double_ended_skip_iterator", since = "1.9.0")]
125 impl<I> DoubleEndedIterator for Skip<I>
127 I: DoubleEndedIterator + ExactSizeIterator,
129 fn next_back(&mut self) -> Option<Self::Item> {
130 if self.len() > 0 { self.iter.next_back() } else { None }
134 fn nth_back(&mut self, n: usize) -> Option<I::Item> {
135 let len = self.len();
137 self.iter.nth_back(n)
140 // consume the original iterator
141 self.iter.nth_back(len - 1);
147 fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
150 Fold: FnMut(Acc, Self::Item) -> R,
153 fn check<T, Acc, R: Try<Ok = Acc>>(
155 mut fold: impl FnMut(Acc, T) -> R,
156 ) -> impl FnMut(Acc, T) -> ControlFlow<R, Acc> {
159 let r = fold(acc, x);
160 if n == 0 { ControlFlow::Break(r) } else { ControlFlow::from_try(r) }
165 if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
168 fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
170 Fold: FnMut(Acc, Self::Item) -> Acc,
173 fn ok<Acc, T>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, T) -> Result<Acc, !> {
174 move |acc, x| Ok(f(acc, x))
177 self.try_rfold(init, ok(fold)).unwrap()
181 #[stable(feature = "fused", since = "1.26.0")]
182 impl<I> FusedIterator for Skip<I> where I: FusedIterator {}
184 #[unstable(issue = "none", feature = "inplace_iteration")]
185 unsafe impl<S: Iterator, I: Iterator> SourceIter for Skip<I>
187 I: SourceIter<Source = S>,
192 unsafe fn as_inner(&mut self) -> &mut S {
193 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
194 unsafe { SourceIter::as_inner(&mut self.iter) }
198 #[unstable(issue = "none", feature = "inplace_iteration")]
199 unsafe impl<I: InPlaceIterable> InPlaceIterable for Skip<I> {}