lending_iterator/lending_iterator/adapters/
skip.rs

1use ::core::num::NonZeroUsize;
2
3/// The <code>impl [LendingIterator]</code> returned by
4/// [`.skip()`][LendingIterator::skip()].
5pub
6struct Skip<I : LendingIterator> {
7    pub(in crate)
8    iter: I,
9
10    pub(in crate)
11    to_skip: Option<NonZeroUsize>,
12}
13
14fn ensure_skipped(it: &mut Skip<impl LendingIterator>) {
15    if let Some(to_skip) = it.to_skip.take() {
16        // nth(n) skips n+1
17        it.iter.nth(to_skip.get() - 1);
18    }
19}
20
21#[gat]
22impl<I : LendingIterator> LendingIterator for Skip<I> {
23    type Item<'next>
24    where
25        Self : 'next,
26    =
27        Item<'next, I>
28    ;
29
30    #[inline]
31    fn next (self: &'_ mut Skip<I>)
32      -> Option<Item<'_, I>>
33    {
34        ensure_skipped(self);
35        self.iter.next()
36    }
37
38    #[inline]
39    fn nth (
40        self: &'_ mut Skip<I>,
41        n: usize,
42    ) -> Option<Item<'_, I>>
43    {
44        ensure_skipped(self);
45        self.iter.nth(n)
46    }
47}