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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
macro_rules! arithmetic_ops { { @new $type:ty, $feature:expr, $trait:ident, $func:ident, () } => { impl core::ops::$trait<$type> for $type { type Output = Self; #[allow(unused_unsafe)] #[inline] fn $func(mut self, rhs: Self) -> Self { for (a, b) in self.iter_mut().zip(rhs.iter()) { *a = core::ops::$trait::$func(*a, b); } self } } impl core::ops::$trait<<$type as $crate::vector::Vector>::Scalar> for $type { type Output = Self; #[inline] fn $func(mut self, rhs: <$type as $crate::vector::Vector>::Scalar) -> Self { for a in self.iter_mut() { *a = core::ops::$trait::$func(*a, rhs); } self } } }; { @assign $type:ty, $feature:expr, $trait:ident, $func:ident, () } => { impl core::ops::$trait<$type> for $type { #[allow(unused_unsafe)] #[inline] fn $func(&mut self, rhs: Self) { for (a, b) in self.iter_mut().zip(rhs.iter()) { core::ops::$trait::$func(a, b); } } } impl core::ops::$trait<<$type as $crate::vector::Vector>::Scalar> for $type { #[inline] fn $func(&mut self, rhs: <$type as $crate::vector::Vector>::Scalar) { for a in self.iter_mut() { core::ops::$trait::$func(a, rhs); } } } }; { @new $type:ty, $feature:expr, $trait:ident, $func:ident, ($op:path) } => { impl core::ops::$trait<$type> for $type { type Output = Self; #[allow(unused_unsafe)] #[inline] fn $func(self, rhs: Self) -> Self { Self(unsafe { $op(self.0, rhs.0) }) } } impl core::ops::$trait<<$type as $crate::vector::Vector>::Scalar> for $type { type Output = Self; #[inline] fn $func(self, rhs: <$type as $crate::vector::Vector>::Scalar) -> Self { self.$func(<$type>::splat(unsafe { $feature }, rhs)) } } }; { @assign $type:ty, $feature:expr, $trait:ident, $func:ident, ($op:path) } => { impl core::ops::$trait<$type> for $type { #[allow(unused_unsafe)] #[inline] fn $func(&mut self, rhs: Self) { self.0 = unsafe { $op(self.0, rhs.0) }; } } impl core::ops::$trait<<$type as $crate::vector::Vector>::Scalar> for $type { #[inline] fn $func(&mut self, rhs: <$type as $crate::vector::Vector>::Scalar) { self.$func(<$type>::splat(unsafe { $feature }, rhs)) } } }; { feature: $feature:expr, for $type:ty: add -> $add_expr:tt, sub -> $sub_expr:tt, mul -> $mul_expr:tt, div -> $div_expr:tt } => { impl core::iter::Sum<$type> for Option<$type> { #[inline] fn sum<I>(mut iter: I) -> Self where I: Iterator<Item = $type>, { if let Some(mut sum) = iter.next() { while let Some(v) = iter.next() { sum += v; } Some(sum) } else { None } } } impl core::iter::Sum<$type> for <$type as $crate::vector::Vector>::Scalar { #[inline] fn sum<I>(iter: I) -> Self where I: Iterator<Item = $type>, { if let Some(sums) = iter.sum::<Option<$type>>() { sums.iter().sum() } else { Default::default() } } } impl core::iter::Product<$type> for Option<$type> { #[inline] fn product<I>(mut iter: I) -> Self where I: Iterator<Item = $type>, { if let Some(mut sum) = iter.next() { while let Some(v) = iter.next() { sum *= v; } Some(sum) } else { None } } } impl core::iter::Product<$type> for <$type as $crate::vector::Vector>::Scalar { #[inline] fn product<I>(iter: I) -> Self where I: Iterator<Item = $type>, { if let Some(sums) = iter.sum::<Option<$type>>() { sums.iter().product() } else { Default::default() } } } arithmetic_ops!{@new $type, $feature, Add, add, $add_expr} arithmetic_ops!{@new $type, $feature, Sub, sub, $sub_expr} arithmetic_ops!{@new $type, $feature, Mul, mul, $mul_expr} arithmetic_ops!{@new $type, $feature, Div, div, $div_expr} arithmetic_ops!{@assign $type, $feature, AddAssign, add_assign, $add_expr} arithmetic_ops!{@assign $type, $feature, SubAssign, sub_assign, $sub_expr} arithmetic_ops!{@assign $type, $feature, MulAssign, mul_assign, $mul_expr} arithmetic_ops!{@assign $type, $feature, DivAssign, div_assign, $div_expr} }; } macro_rules! as_slice { { $type:ty } => { impl AsRef<[<$type as crate::vector::Vector>::Scalar]> for $type { #[inline] fn as_ref(&self) -> &[<$type as crate::vector::Vector>::Scalar] { use crate::vector::Vector; self.as_slice() } } impl AsMut<[<$type as crate::vector::Vector>::Scalar]> for $type { #[inline] fn as_mut(&mut self) -> &mut [<$type as crate::vector::Vector>::Scalar] { use crate::vector::Vector; self.as_slice_mut() } } impl core::ops::Deref for $type { type Target = [<Self as crate::vector::Vector>::Scalar]; #[inline] fn deref(&self) -> &Self::Target { self.as_slice() } } impl core::ops::DerefMut for $type { #[inline] fn deref_mut(&mut self) -> &mut <Self as core::ops::Deref>::Target { self.as_slice_mut() } } } }