generator_combinator/
iter.rs

1use crate::Generator;
2
3/// Provides iterable access to the range of values represented by the [`Generator`]
4pub struct StringIter<'a> {
5    /// The generator to be used
6    c: &'a Generator,
7
8    /// The total number of values for `c`, equal to `c.len()`
9    n: u128,
10
11    /// The current value of the iterator. The first value is 0, the last is `n-1`.
12    i: u128,
13}
14
15impl<'a> Iterator for StringIter<'a> {
16    type Item = String;
17
18    fn next(&mut self) -> Option<Self::Item> {
19        if self.i == self.n {
20            None
21        } else {
22            self.i += 1;
23            Some(self.c.generate_one(self.i - 1))
24        }
25    }
26}
27
28#[cfg(feature = "with_rand")]
29impl<'a> StringIter<'a> {
30    /// Generates a random value in the [`Generator`]'s domain
31    pub fn random(&self) -> String {
32        let num = rand::random::<u128>() % self.n;
33        self.c.generate_one(num)
34    }
35}
36
37impl<'a> From<&'a Generator> for StringIter<'a> {
38    fn from(c: &'a Generator) -> Self {
39        Self {
40            c,
41            n: c.len(),
42            i: 0,
43        }
44    }
45}