non_empty_iter/
step_by.rs

1//! Stepping non-empty iterators by the custom amount.
2
3use core::iter;
4
5use non_zero_size::Size;
6
7use crate::non_empty::NonEmptyIterator;
8
9/// Represents non-empty iterators for stepping non-empty iterators by the custom amount.
10///
11/// This `struct` is created by the [`step_by`] method on [`NonEmptyIterator`].
12/// See its documentation for more.
13///
14/// [`step_by`]: NonEmptyIterator::step_by
15#[derive(Debug, Clone)]
16#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
17pub struct StepBy<I: NonEmptyIterator> {
18    non_empty: I,
19    step: Size,
20}
21
22impl<I: NonEmptyIterator> StepBy<I> {
23    /// Constructs [`Self`].
24    pub const fn new(non_empty: I, step: Size) -> Self {
25        Self { non_empty, step }
26    }
27}
28
29impl<I: NonEmptyIterator> IntoIterator for StepBy<I> {
30    type Item = I::Item;
31
32    type IntoIter = iter::StepBy<I::IntoIter>;
33
34    fn into_iter(self) -> Self::IntoIter {
35        self.non_empty.into_iter().step_by(self.step.get())
36    }
37}
38
39unsafe impl<I: NonEmptyIterator> NonEmptyIterator for StepBy<I> {}