]> git.nega.tv - josh/narcissus/commitdiff
Add extra vector operators
authorJoshua Simmons <josh@nega.tv>
Wed, 7 Sep 2022 20:55:41 +0000 (22:55 +0200)
committerJoshua Simmons <josh@nega.tv>
Wed, 7 Sep 2022 20:55:41 +0000 (22:55 +0200)
Add Add/Sub/Mul/Div by scalar
Add Neg

narcissus-maths/src/lib.rs

index 23050f86e8b293ad8b0e6edc6fd70f93491c5b97..174f69f3a8638c303e54e3a8604347b6adcab0ea 100644 (file)
@@ -141,5 +141,81 @@ macro_rules! impl_vector {
                 Self::dot(self, self)
             }
         }
+
+        impl std::ops::Neg for $name {
+            type Output = $name;
+            #[inline(always)]
+            fn neg(self) -> Self::Output {
+                self.map(|x| -x)
+            }
+        }
+
+        impl std::ops::Add<$t> for $name {
+            type Output = $name;
+            #[inline(always)]
+            fn add(self, rhs: $t) -> Self::Output {
+                self.map(|x| x + rhs)
+            }
+        }
+
+        impl std::ops::Sub<$t> for $name {
+            type Output = $name;
+            #[inline(always)]
+            fn sub(self, rhs: $t) -> Self::Output {
+                self.map(|x| x - rhs)
+            }
+        }
+
+        impl std::ops::Mul<$t> for $name {
+            type Output = $name;
+            #[inline(always)]
+            fn mul(self, rhs: $t) -> Self::Output {
+                self.map(|x| x * rhs)
+            }
+        }
+
+        impl std::ops::Mul<$name> for $t {
+            type Output = $name;
+            #[inline(always)]
+            fn mul(self, rhs: $name) -> Self::Output {
+                rhs.map(|x| self * x)
+            }
+        }
+
+        impl std::ops::Div<$t> for $name {
+            type Output = $name;
+            #[inline(always)]
+            fn div(self, rhs: $t) -> Self::Output {
+                self.map(|x| x / rhs)
+            }
+        }
+
+        impl std::ops::AddAssign<$t> for $name {
+            #[inline(always)]
+            fn add_assign(&mut self, rhs: $t) {
+                *self = *self + rhs;
+            }
+        }
+
+        impl std::ops::SubAssign<$t> for $name {
+            #[inline(always)]
+            fn sub_assign(&mut self, rhs: $t) {
+                *self = *self - rhs;
+            }
+        }
+
+        impl std::ops::MulAssign<$t> for $name {
+            #[inline(always)]
+            fn mul_assign(&mut self, rhs: $t) {
+                *self = *self * rhs;
+            }
+        }
+
+        impl std::ops::DivAssign<$t> for $name {
+            #[inline(always)]
+            fn div_assign(&mut self, rhs: $t) {
+                *self = *self / rhs;
+            }
+        }
     };
 }