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
72
73
74
75
76
77
78
79
80
81
82
83
84
use super::Signal;
use core::mem;
#[derive(Clone)]
pub struct Const<T> {
pub(super) value: T,
}
impl<T: Clone> Signal for Const<T> {
type Type = T;
#[inline]
fn next(&mut self) -> Self::Type {
self.value.clone()
}
}
#[derive(Clone)]
pub struct Gen<F> {
pub(super) gen: F,
}
impl<F, T> Signal for Gen<F>
where
F: FnMut() -> T,
{
type Type = T;
#[inline]
fn next(&mut self) -> Self::Type {
(self.gen)()
}
}
#[derive(Clone)]
pub struct Successors<T, F> {
pub(super) state: T,
pub(super) successor: F,
}
impl<T, F> Signal for Successors<T, F>
where
F: FnMut(&T) -> T,
{
type Type = T;
#[inline]
fn next(&mut self) -> Self::Type {
let next_state = (self.successor)(&self.state);
mem::replace(&mut self.state, next_state)
}
}
pub struct Cycle<I> {
pub(super) orig: I,
pub(super) iter: I,
}
impl<I> Signal for Cycle<I>
where
I: Iterator + Clone,
{
type Type = I::Item;
fn next(&mut self) -> Self::Type {
match self.iter.next() {
Some(item) => item,
None => {
self.iter = self.orig.clone();
self.iter
.next()
.expect("The iterator always returns `None`")
}
}
}
}