orx_iterable/sources/
repeat_n.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::Iterable;

/// An iterable which yields the same value `n` times.
pub struct RepeatN<T>
where
    T: Clone,
{
    pub(crate) value: T,
    pub(crate) count: usize,
}

impl<T> Iterable for RepeatN<T>
where
    T: Clone,
{
    type Item = T;

    type Iter = core::iter::RepeatN<T>;

    fn iter(&self) -> Self::Iter {
        core::iter::repeat_n(self.value.clone(), self.count)
    }
}

impl<T> Iterable for core::iter::RepeatN<T>
where
    T: Clone,
{
    type Item = T;

    type Iter = core::iter::RepeatN<T>;

    fn iter(&self) -> Self::Iter {
        self.clone()
    }
}

/// Creates an iterable which yields the same value `n` times.
pub fn repeat_n<T>(value: T, count: usize) -> RepeatN<T>
where
    T: Clone,
{
    RepeatN { value, count }
}