Skip to main content

khal_std/
iter.rs

1use core::ops::Range;
2
3/// A custom replacement for `(a..b).step_by(c)`.
4/// This is useful as the iterator combinator adds some early-exists that break uniform control
5/// flow; this is incompatible with using barriers when targetting WebGpu on the browser.
6pub struct StepRng {
7    /// Current position of the iterator.
8    pub from: u32,
9    /// Exclusive upper bound.
10    pub end: u32,
11    /// Step size between iterations.
12    pub step: u32,
13}
14
15impl StepRng {
16    /// Creates a new stepped range iterator over `rng` with the given `step` size.
17    pub fn new(rng: Range<u32>, step: u32) -> Self {
18        Self {
19            from: rng.start,
20            end: rng.end,
21            step,
22        }
23    }
24}
25
26impl Iterator for StepRng {
27    type Item = u32;
28    #[inline]
29    fn next(&mut self) -> Option<Self::Item> {
30        let result = self.from;
31        if result >= self.end {
32            None
33        } else {
34            self.from += self.step;
35            Some(result)
36        }
37    }
38}