gat_lending_iterator/adapters/
step_by.rs

1use crate::LendingIterator;
2
3/// A lending iterator for stepping lending iterators by a custom amount.
4///
5/// This `struct` is created by the [`step_by`] method on [`LendingIterator`]. See
6/// its documentation for more.
7///
8/// [`LendingIterator`]: crate::LendingIterator
9/// [`step_by`]: crate::LendingIterator::step_by
10#[derive(Clone, Debug)]
11#[must_use = "iterators are lazy and do nothing unless consumed"]
12pub struct StepBy<I> {
13    iter: I,
14    step: usize,
15    first_take: bool,
16}
17
18impl<I> StepBy<I> {
19    #[inline]
20    pub(crate) fn new(iter: I, step: usize) -> StepBy<I> {
21        assert!(step != 0);
22        StepBy {
23            iter,
24            step: step - 1,
25            first_take: true,
26        }
27    }
28}
29
30impl<I> LendingIterator for StepBy<I>
31where
32    I: LendingIterator,
33{
34    type Item<'a> = I::Item<'a>
35        where
36            Self: 'a
37    ;
38
39    #[inline]
40    fn next(&mut self) -> Option<Self::Item<'_>> {
41        if self.first_take {
42            self.first_take = false;
43            self.iter.next()
44        } else {
45            self.iter.nth(self.step)
46        }
47    }
48}