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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use core::ops::*;

use crate::prelude::*;

////////////////////////////////////////////////////////////////////////////////
// Vector + T => Vector
////////////////////////////////////////////////////////////////////////////////

macro_rules! _impl_op_scalar {
    ($meth:ident, impl $trt:ident<$rhs:ty> for $lhs:ty where T: $($bound:tt)+) => {
        impl<'a, T: Base, const N: usize> $trt<$rhs> for $lhs
        where
            T: $($bound)+
        {
            type Output = Vector<T, N>;

            fn $meth(self, other: $rhs) -> Self::Output {
                self.map(|n| n.$meth(other))
            }
        }
    };
}

macro_rules! impl_op_scalar {
    ($trt:ident, $meth:ident) => {
        _impl_op_scalar! { $meth, impl $trt<    T> for  Vector<T, N> where T: $trt<Output = T>        } //  Vector +  T
        _impl_op_scalar! { $meth, impl $trt<&'a T> for  Vector<T, N> where T: $trt<&'a T, Output = T> } //  Vector + &T
        _impl_op_scalar! { $meth, impl $trt<    T> for &Vector<T, N> where T: $trt<Output = T>        } // &Vector +  T
        _impl_op_scalar! { $meth, impl $trt<&'a T> for &Vector<T, N> where T: $trt<&'a T, Output = T> } // &Vector + &T
    };
}

impl_op_scalar! { Add, add }
impl_op_scalar! { Sub, sub }
impl_op_scalar! { Mul, mul }
impl_op_scalar! { Div, div }
impl_op_scalar! { Rem, rem }

impl_op_scalar! { BitAnd, bitand }
impl_op_scalar! { BitOr, bitor }
impl_op_scalar! { BitXor, bitxor }
impl_op_scalar! { Shl, shl }
impl_op_scalar! { Shr, shr }

////////////////////////////////////////////////////////////////////////////////
// Vector += T
////////////////////////////////////////////////////////////////////////////////

macro_rules! _impl_op_assign_scalar {
    ($meth:ident, impl $trt:ident<$rhs:ty> where T: $($bound:tt)+) => {
        impl<'a, T: Base, const N: usize> $trt<$rhs> for Vector<T, N>
        where
            T: $($bound)+
        {
            fn $meth(&mut self, other: $rhs) {
                for i in 0..N {
                    self[i].$meth(other);
                }
            }
        }
    };
}

macro_rules! impl_op_assign_scalar {
    ($trt:ident, $meth:ident) => {
        _impl_op_assign_scalar! { $meth, impl $trt<    T> where T: $trt        } // Vector +=  T
        _impl_op_assign_scalar! { $meth, impl $trt<&'a T> where T: $trt<&'a T> } // Vector += &T
    };
}

impl_op_assign_scalar! { AddAssign, add_assign }
impl_op_assign_scalar! { SubAssign, sub_assign }
impl_op_assign_scalar! { MulAssign, mul_assign }
impl_op_assign_scalar! { DivAssign, div_assign }
impl_op_assign_scalar! { RemAssign, rem_assign }

impl_op_assign_scalar! { BitAndAssign, bitand_assign }
impl_op_assign_scalar! { BitOrAssign, bitor_assign }
impl_op_assign_scalar! { BitXorAssign, bitxor_assign }
impl_op_assign_scalar! { ShlAssign, shl_assign }
impl_op_assign_scalar! { ShrAssign, shr_assign }

////////////////////////////////////////////////////////////////////////////////
// Vector + Vector => Vector
////////////////////////////////////////////////////////////////////////////////

macro_rules! _impl_op {
    ($meth:ident, impl $trt:ident<$rhs:ty> for $lhs:ty, $($deref:tt)?) => {
        impl<T: Base, const N: usize> $trt<$rhs> for $lhs
        where
            T: $trt<Output = T>
        {
            type Output = Vector<T, N>;

            fn $meth(self, other: $rhs) -> Self::Output {
                let mut vector = $($deref)? self;
                for i in 0..N {
                    vector[i] = self[i].$meth(other[i]);
                }
                vector
            }
        }
    };
}

macro_rules! impl_op {
    ($trt:ident, $meth:ident) => {
        _impl_op! { $meth, impl $trt< Vector<T, N>> for  Vector<T, N>,   } //  Vector +  Vector
        _impl_op! { $meth, impl $trt< Vector<T, N>> for &Vector<T, N>, * } //  Vector + &Vector
        _impl_op! { $meth, impl $trt<&Vector<T, N>> for  Vector<T, N>,   } // &Vector +  Vector
        _impl_op! { $meth, impl $trt<&Vector<T, N>> for &Vector<T, N>, * } // &Vector + &Vector
    };
}

impl_op! { Add, add }
impl_op! { Sub, sub }

////////////////////////////////////////////////////////////////////////////////
// Vector += Vector
////////////////////////////////////////////////////////////////////////////////

macro_rules! impl_op_assign {
    (impl $trt:ident<$rhs:ty>, $meth:ident) => {
        impl<T: Base, const N: usize> $trt<$rhs> for Vector<T, N>
        where
            T: $trt,
        {
            fn $meth(&mut self, other: $rhs) {
                for i in 0..N {
                    self[i].$meth(other[i]);
                }
            }
        }
    };
}

impl_op_assign! { impl AddAssign< Vector<T, N>>, add_assign }
impl_op_assign! { impl AddAssign<&Vector<T, N>>, add_assign }
impl_op_assign! { impl SubAssign< Vector<T, N>>, sub_assign }
impl_op_assign! { impl SubAssign<&Vector<T, N>>, sub_assign }

////////////////////////////////////////////////////////////////////////////////
// -Vector
////////////////////////////////////////////////////////////////////////////////

macro_rules! impl_op_unary {
    (impl $trt:ident, $meth:ident for $lhs:ty) => {
        impl<T: Base, const N: usize> $trt for $lhs
        where
            T: $trt<Output = T>,
        {
            type Output = Vector<T, N>;

            fn $meth(self) -> Self::Output {
                self.map($trt::$meth)
            }
        }
    };
}

impl_op_unary! { impl Neg, neg for  Vector<T, N> }
impl_op_unary! { impl Neg, neg for &Vector<T, N> }
impl_op_unary! { impl Not, not for  Vector<T, N> }
impl_op_unary! { impl Not, not for &Vector<T, N> }