use crate::LendingIterator;
#[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));
}
}