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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use signalo_traits::source::Source;
use source::constant::Constant;
use source::take::Take;
#[derive(Clone, Debug)]
pub struct Repeat<T> {
inner: Take<Constant<T>>,
}
impl<T> Repeat<T> {
#[inline]
pub fn new(value: T, count: usize) -> Self {
let constant = Constant::new(value);
let inner = Take::new(constant, count);
Self { inner }
}
}
impl<T> Source for Repeat<T>
where
T: Clone,
Constant<T>: Source<Output = T>,
Take<Constant<T>>: Source<Output = T>,
{
type Output = T;
#[inline]
fn source(&mut self) -> Option<Self::Output> {
self.inner.source()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let mut source = Repeat::new(42, 5);
let mut subject: Vec<usize> = vec![];
while let Some(value) = source.source() {
subject.push(value);
}
let expected = vec![42; 5];
assert_eq!(subject, expected);
}
}