1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use ::core::num::NonZeroUsize;

/// The <code>impl [LendingIterator]</code> returned by
/// [`.skip()`][LendingIterator::skip()].
pub
struct Skip<I : LendingIterator> {
    pub(in crate)
    iter: I,

    pub(in crate)
    to_skip: Option<NonZeroUsize>,
}

fn ensure_skipped(it: &mut Skip<impl LendingIterator>) {
    if let Some(to_skip) = it.to_skip.take() {
        // nth(n) skips n+1
        it.iter.nth(to_skip.get() - 1);
    }
}

#[gat]
impl<I : LendingIterator> LendingIterator for Skip<I> {
    type Item<'next>
    where
        Self : 'next,
    =
        Item<'next, I>
    ;

    #[inline]
    fn next (self: &'_ mut Skip<I>)
      -> Option<Item<'_, I>>
    {
        ensure_skipped(self);
        self.iter.next()
    }

    #[inline]
    fn nth (
        self: &'_ mut Skip<I>,
        n: usize,
    ) -> Option<Item<'_, I>>
    {
        ensure_skipped(self);
        self.iter.nth(n)
    }
}