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
macro_rules! m_ops_base {
    ( $Tuple:ident { $($T:ident . $idx:tt),* } : $op:ident . $fn:ident ) =>
    (
        impl<$($T),*> $op for $Tuple<$($T),*> where $( $T: $op ),* {
            type Output = $Tuple<$($T::Output),*>;
            fn $fn(self, rhs: Self) -> Self::Output {
                $Tuple( $(self.$idx.$fn( rhs.$idx ) ),* )
            }
        }
        impl<T> $op<T> for $Tuple<$(A!(T, $T)),*> where T: $op + Clone {
            type Output = $Tuple<$(<A!(T, $T) as $op>::Output),*>;
            fn $fn(self, rhs: T) -> Self::Output {
                $Tuple( $(self.$idx.$fn( rhs.clone() ) ),* )
            }
        }
    )
}
macro_rules! m_ops_base_assign {
    ( $Tuple:ident { $($T:ident . $idx:tt),* } : $op:ident . $fn:ident ) =>
    (
        impl<$($T),*> $op for $Tuple<$($T),*> where $( $T: $op ),* {
            fn $fn(&mut self, rhs: Self) {
             $( self.$idx.$fn(rhs.$idx); )*
            }
        }
        impl<T> $op<T> for $Tuple<$(A!(T, $T)),*> where T: $op + Clone {
            fn $fn(&mut self, rhs: T) {
             $( self.$idx.$fn(rhs.clone()); )*
            }
        }
    )
}
macro_rules! m_ops_all {
    ( $Tuple:ident { $($T:ident . $idx:tt),* } :
        $op:ident.$fn:ident, $op_a:ident.$fn_a:ident) =>
    ( 
        m_ops_base!( $Tuple { $($T . $idx),* } : $op.$fn );
        m_ops_base_assign!( $Tuple { $($T . $idx),* } : $op_a.$fn_a );
    )
}
macro_rules! m_ops {
    ($($Tuple:ident { $($T:ident . $idx:tt),* } )*) => ($(
        m_ops_all!( $Tuple { $($T . $idx),* } : Add.add, AddAssign.add_assign );
        m_ops_all!( $Tuple { $($T . $idx),* } : Sub.sub, SubAssign.sub_assign );
        m_ops_all!( $Tuple { $($T . $idx),* } : Mul.mul, MulAssign.mul_assign );
        m_ops_all!( $Tuple { $($T . $idx),* } : Div.div, DivAssign.div_assign );
        
        impl<$($T),*> ops::Neg for $Tuple<$($T),*> where $( $T: ops::Neg ),* {
            type Output = $Tuple<$($T::Output),*>;
            fn neg(self) -> Self::Output {
                $Tuple( $(self.$idx.neg()),* )
            }
        }
        impl<T> ops::Index<usize> for $Tuple<$(A!(T,$T)),*> {
            type Output = T;
            fn index(&self, index: usize) -> &T {
                match index {
                 $( $idx => &self.$idx, )*
                    _ => panic!("index {} out of bounds. len is {}.", index, Self::N)
                }
            }
        }
        impl<T> ops::IndexMut<usize> for $Tuple<$(A!(T,$T)),*> {
            fn index_mut(&mut self, index: usize) -> &mut T {
                match index {
                 $( $idx => &mut self.$idx, )*
                    _ => panic!("index {} out of bounds. len is {}.", index, Self::N)
                }
            }
        }
    )*)
}