orx_iterable/transformations/
stepped_by.rs

1use crate::{Collection, CollectionMut, Iterable};
2use core::marker::PhantomData;
3use orx_self_or::SoM;
4
5/// Wraps an `Iterable` and creates a new `Iterable` which yields elements of
6/// the original iterable by stepping by a given step size.
7pub struct SteppedBy<I>
8where
9    I: Iterable,
10{
11    pub(crate) it: I,
12    pub(crate) step: usize,
13}
14
15impl<I> Iterable for SteppedBy<I>
16where
17    I: Iterable,
18{
19    type Item = I::Item;
20
21    type Iter = core::iter::StepBy<I::Iter>;
22
23    fn iter(&self) -> Self::Iter {
24        self.it.iter().step_by(self.step)
25    }
26}
27
28// col
29
30/// Wraps an `Collection` and creates a new `Collection` which yields elements of
31/// the original iterable by stepping by a given step size.
32pub struct SteppedByCol<I, E>
33where
34    I: Collection,
35    E: SoM<I>,
36{
37    pub(crate) it: E,
38    pub(crate) step: usize,
39    pub(crate) phantom: PhantomData<I>,
40}
41
42impl<'a, I, E> Iterable for &'a SteppedByCol<I, E>
43where
44    I: Collection,
45    E: SoM<I>,
46{
47    type Item = &'a I::Item;
48
49    type Iter = core::iter::StepBy<<I::Iterable<'a> as Iterable>::Iter>;
50
51    fn iter(&self) -> Self::Iter {
52        self.it.get_ref().iter().step_by(self.step)
53    }
54}
55
56impl<I, E> Collection for SteppedByCol<I, E>
57where
58    I: Collection,
59    E: SoM<I>,
60{
61    type Item = I::Item;
62
63    type Iterable<'i>
64        = &'i Self
65    where
66        Self: 'i;
67
68    fn as_iterable(&self) -> Self::Iterable<'_> {
69        self
70    }
71}
72
73impl<I, E> CollectionMut for SteppedByCol<I, E>
74where
75    I: CollectionMut,
76    E: SoM<I>,
77{
78    type IterMut<'i>
79        = core::iter::StepBy<I::IterMut<'i>>
80    where
81        Self: 'i;
82
83    fn iter_mut(&mut self) -> Self::IterMut<'_> {
84        self.it.get_mut().iter_mut().step_by(self.step)
85    }
86}