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
use reducer::*;

macro_rules! document_reducer_for_tuples {
    ( ($head:ident), $( $body:tt )+ ) => {
        /// Updates all reducers in the tuple in order.
        ///
        /// Currently implemented for tuples of up to 12 elements.
        $( $body )+
    };

    ( ($head:ident $(, $tail:ident )+), $( $body:tt )+ ) => {
        #[doc(hidden)]
        $( $body )+
    };
}

macro_rules! impl_reducer_for_tuples {
    () => {};

    ( $head:ident $(, $tail:ident )* $(,)* ) => {
        document_reducer_for_tuples!(($head $(, $tail )*),
            impl<A, $head, $( $tail, )*> Reducer<A> for ($head, $( $tail, )*)
            where
                A: Clone,
                $head: Reducer<A>,
                $( $tail: Reducer<A>, )*
            {
                fn reduce(&mut self, action: A) {
                    let ($head, $( $tail, )*) = self;
                    $head.reduce(action.clone());
                    $( $tail.reduce(action.clone()); )*
                }
            }
        );

        impl_reducer_for_tuples!($( $tail, )*);
    };
}

impl_reducer_for_tuples!(_12, _11, _10, _09, _08, _07, _06, _05, _04, _03, _02, _01);

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_reducer_for_tuples {
        () => {};

        ( $head:ident $(, $tail:ident )* $(,)* ) => {
            #[derive(Debug, Default, Clone, Eq, PartialEq)]
            struct $head<A: 'static> {
                inner: MockReducer<A>,
            }

            impl<A: 'static + Clone> Reducer<A> for $head<A> {
                fn reduce(&mut self, action: A) {
                    self.inner.reduce(action);
                }
            }

            #[test]
            fn $head() {
                let mut states = ($head::default(), $( $tail::default(), )*);

                states.reduce(5);
                states.reduce(1);
                states.reduce(3);

                let ($head, $( $tail, )*) = states;

                assert_eq!($head.inner, MockReducer::new(vec![5, 1, 3]));
                $( assert_eq!($tail.inner, MockReducer::new(vec![5, 1, 3])); )*
            }

            test_reducer_for_tuples!($( $tail, )*);
        };
    }

    test_reducer_for_tuples!(_12, _11, _10, _09, _08, _07, _06, _05, _04, _03, _02, _01);
}