1 use crate::intrinsics::unlikely;
2 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
3 use crate::ops::{ControlFlow, Try};
5 /// An iterator that skips over `n` elements of `iter`.
7 /// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
8 /// documentation for more.
10 /// [`skip`]: Iterator::skip
11 /// [`Iterator`]: trait.Iterator.html
12 #[derive(Clone, Debug)]
13 #[must_use = "iterators are lazy and do nothing unless consumed"]
14 #[stable(feature = "rust1", since = "1.0.0")]
21 pub(in crate::iter) fn new(iter: I, n: usize) -> Skip<I> {
26 #[stable(feature = "rust1", since = "1.0.0")]
27 impl<I> Iterator for Skip<I>
31 type Item = <I as Iterator>::Item;
34 fn next(&mut self) -> Option<I::Item> {
35 if unlikely(self.n > 0) {
36 self.iter.nth(crate::mem::take(&mut self.n))
43 fn nth(&mut self, n: usize) -> Option<I::Item> {
45 let skip: usize = crate::mem::take(&mut self.n);
46 // Checked add to handle overflow case.
47 let n = match skip.checked_add(n) {
50 // In case of overflow, load skip value, before loading `n`.
51 // Because the amount of elements to iterate is beyond `usize::MAX`, this
52 // is split into two `nth` calls where the `skip` `nth` call is discarded.
53 self.iter.nth(skip - 1)?;
57 // Load nth element including skip.
65 fn count(mut self) -> usize {
68 if self.iter.nth(self.n - 1).is_none() {
76 fn last(mut self) -> Option<I::Item> {
79 self.iter.nth(self.n - 1)?;
85 fn size_hint(&self) -> (usize, Option<usize>) {
86 let (lower, upper) = self.iter.size_hint();
88 let lower = lower.saturating_sub(self.n);
89 let upper = match upper {
90 Some(x) => Some(x.saturating_sub(self.n)),
98 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
101 Fold: FnMut(Acc, Self::Item) -> R,
102 R: Try<Output = Acc>,
108 if self.iter.nth(n - 1).is_none() {
112 self.iter.try_fold(init, fold)
116 fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
118 Fold: FnMut(Acc, Self::Item) -> Acc,
122 if self.iter.nth(self.n - 1).is_none() {
126 self.iter.fold(init, fold)
130 #[rustc_inherit_overflow_checks]
131 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
133 let step_one = self.n.saturating_add(rem);
135 match self.iter.advance_by(step_one) {
137 rem -= step_one - self.n;
141 let advanced_without_skip = advanced.saturating_sub(self.n);
142 self.n = self.n.saturating_sub(advanced);
143 return if n == 0 { Ok(()) } else { Err(advanced_without_skip) };
147 // step_one calculation may have saturated
148 if unlikely(rem > 0) {
149 return match self.iter.advance_by(rem) {
162 #[stable(feature = "rust1", since = "1.0.0")]
163 impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}
165 #[stable(feature = "double_ended_skip_iterator", since = "1.9.0")]
166 impl<I> DoubleEndedIterator for Skip<I>
168 I: DoubleEndedIterator + ExactSizeIterator,
170 fn next_back(&mut self) -> Option<Self::Item> {
171 if self.len() > 0 { self.iter.next_back() } else { None }
175 fn nth_back(&mut self, n: usize) -> Option<I::Item> {
176 let len = self.len();
178 self.iter.nth_back(n)
181 // consume the original iterator
182 self.iter.nth_back(len - 1);
188 fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
191 Fold: FnMut(Acc, Self::Item) -> R,
192 R: Try<Output = Acc>,
194 fn check<T, Acc, R: Try<Output = Acc>>(
196 mut fold: impl FnMut(Acc, T) -> R,
197 ) -> impl FnMut(Acc, T) -> ControlFlow<R, Acc> {
200 let r = fold(acc, x);
201 if n == 0 { ControlFlow::Break(r) } else { ControlFlow::from_try(r) }
206 if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
209 impl_fold_via_try_fold! { rfold -> try_rfold }
212 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
213 let min = crate::cmp::min(self.len(), n);
214 return match self.iter.advance_back_by(min) {
215 ret @ Ok(_) if n <= min => ret,
217 _ => panic!("ExactSizeIterator contract violation"),
222 #[stable(feature = "fused", since = "1.26.0")]
223 impl<I> FusedIterator for Skip<I> where I: FusedIterator {}
225 #[unstable(issue = "none", feature = "inplace_iteration")]
226 unsafe impl<I> SourceIter for Skip<I>
230 type Source = I::Source;
233 unsafe fn as_inner(&mut self) -> &mut I::Source {
234 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
235 unsafe { SourceIter::as_inner(&mut self.iter) }
239 #[unstable(issue = "none", feature = "inplace_iteration")]
240 unsafe impl<I: InPlaceIterable> InPlaceIterable for Skip<I> {}