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
use reactor::*;
macro_rules! document_reactor_for_tuples {
( ($head:ident), $( $body:tt )+ ) => {
$( $body )+
};
( ($head:ident $(, $tail:ident )+), $( $body:tt )+ ) => {
#[doc(hidden)]
$( $body )+
};
}
macro_rules! impl_reactor_for_tuples {
() => {};
( $head:ident $(, $tail:ident )* $(,)* ) => {
document_reactor_for_tuples!(($head $(, $tail )*),
impl<S, $head, $( $tail, )*> Reactor<S> for ($head, $( $tail, )*)
where
$head: Reactor<S>,
$( $tail: Reactor<S>, )*
{
type Output = ($head::Output, $( $tail::Output, )*);
fn react(&self, state: &S) -> Self::Output {
let ($head, $( $tail, )*) = self;
($head.react(state), $( $tail.react(state), )*)
}
}
);
impl_reactor_for_tuples!($( $tail, )*);
};
}
impl_reactor_for_tuples!(_12, _11, _10, _09, _08, _07, _06, _05, _04, _03, _02, _01);
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_reactor_for_tuples {
() => {};
( $head:ident $(, $tail:ident )* $(,)* ) => {
#[derive(Debug, Default, Clone, Eq, PartialEq)]
struct $head<S: Clone> {
value: S,
}
impl<S: Clone> $head<S> {
fn new(value: S) -> Self {
$head { value }
}
}
impl<S: Clone> Reactor<S> for $head<S> {
type Output = Self;
fn react(&self, state: &S) -> Self::Output {
$head::new(state.clone())
}
}
#[test]
fn $head() {
let reactor = ($head::default(), $( $tail::default(), )*);
assert_eq!(reactor.react(&5), ($head::new(5), $( $tail::new(5), )*));
assert_eq!(reactor.react(&1), ($head::new(1), $( $tail::new(1), )*));
assert_eq!(reactor.react(&3), ($head::new(3), $( $tail::new(3), )*));
}
test_reactor_for_tuples!($( $tail, )*);
};
}
test_reactor_for_tuples!(_12, _11, _10, _09, _08, _07, _06, _05, _04, _03, _02, _01);
}