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
48
49
50
use crate::LendingIterator;

/// A lending iterator that skips over the first `n` items of `iter`.
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Skip<I> {
    iter: I,
    n: usize,
}

impl<I> Skip<I> {
    pub(crate) fn new(iter: I, n: usize) -> Skip<I> {
        Skip { iter, n }
    }
}

impl<I> LendingIterator for Skip<I>
where
    I: LendingIterator,
{
    type Item<'a> = I::Item<'a> where I: 'a;

    #[inline]
    fn next(&mut self) -> Option<I::Item<'_>> {
        if self.n > 0 {
            self.iter.nth(core::mem::take(&mut self.n))
        } else {
            self.iter.next()
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.iter.size_hint();

        let lower = lower.saturating_sub(self.n);
        let upper = upper.map(|x| x.saturating_sub(self.n));
        (lower, upper)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ToLendingIterator;
    #[test]
    fn test() {
        assert_eq!((0..5).into_lending().skip(1).nth(1), (0..5).skip(1).nth(1));
    }
}