From bb7a9443e00117bfd4dc68ff409abf70e69a9380 Mon Sep 17 00:00:00 2001 From: Markus Mayer Date: Wed, 17 Jul 2024 16:57:18 +0200 Subject: [PATCH 1/2] Add commutative operations on primitive types Addresses #105 --- src/vector.rs | 1 + src/vector/commutative.rs | 22 +++++++++++++++++++++ src/vector/vector2d.rs | 39 +++++++++++++++++++++++++++++++++++++ src/vector/vector3d.rs | 41 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/vector/commutative.rs diff --git a/src/vector.rs b/src/vector.rs index 19cdec5..d164485 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -3,6 +3,7 @@ //! All vectors types impl the [Vector] trait, and all vector components //! impl the [Component] trait. +mod commutative; mod component; mod iter; mod vector2d; diff --git a/src/vector/commutative.rs b/src/vector/commutative.rs new file mode 100644 index 0000000..9080e1f --- /dev/null +++ b/src/vector/commutative.rs @@ -0,0 +1,22 @@ +/// Implements standard commutative operations such as Add and Mul on primitive types. +macro_rules! impl_commutative { + ($vector:ident, $component:ident) => { + impl Add<$vector<$component>> for $component { + type Output = $vector<$component>; + + fn add(self, rhs: $vector<$component>) -> Self::Output { + <$vector<$component> as Add<$component>>::add(rhs, self) + } + } + + impl Mul<$vector<$component>> for $component { + type Output = $vector<$component>; + + fn mul(self, rhs: $vector<$component>) -> Self::Output { + rhs.mul(self) + } + } + }; +} + +pub(crate) use impl_commutative; diff --git a/src/vector/vector2d.rs b/src/vector/vector2d.rs index 0611379..91acb92 100644 --- a/src/vector/vector2d.rs +++ b/src/vector/vector2d.rs @@ -1,6 +1,8 @@ //! 2-dimensional vector use super::{Component, Vector, Vector3d}; +use crate::vector::commutative::impl_commutative; +use crate::F32; use core::{ iter::FromIterator, ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign}, @@ -27,6 +29,15 @@ pub type U32x2 = Vector2d; /// 2-dimensional XY vector of `f32` values pub type F32x2 = Vector2d; +impl_commutative!(Vector2d, i8); +impl_commutative!(Vector2d, i16); +impl_commutative!(Vector2d, i32); +impl_commutative!(Vector2d, u8); +impl_commutative!(Vector2d, u16); +impl_commutative!(Vector2d, u32); +impl_commutative!(Vector2d, f32); +impl_commutative!(Vector2d, F32); + /// 2-dimensional vector #[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct Vector2d { @@ -194,6 +205,20 @@ where } } +impl Add for Vector2d +where + C: Component, +{ + type Output = Self; + + fn add(self, rhs: C) -> Self { + Self { + x: self.x + rhs, + y: self.y + rhs, + } + } +} + impl Sub for Vector2d where C: Component, @@ -217,6 +242,20 @@ where } } +impl Sub for Vector2d +where + C: Component, +{ + type Output = Self; + + fn sub(self, rhs: C) -> Self { + Self { + x: self.x - rhs, + y: self.y - rhs, + } + } +} + impl Mul for Vector2d where C: Component, diff --git a/src/vector/vector3d.rs b/src/vector/vector3d.rs index 09151f0..e6cc668 100644 --- a/src/vector/vector3d.rs +++ b/src/vector/vector3d.rs @@ -1,6 +1,6 @@ //! 3-dimensional vector -use super::{Component, Vector, Vector2d}; +use super::{commutative::impl_commutative, Component, Vector, Vector2d}; use crate::F32; use core::{ iter::FromIterator, @@ -28,6 +28,15 @@ pub type U32x3 = Vector3d; /// 3-dimensional XYZ vector of `f32` values pub type F32x3 = Vector3d; +impl_commutative!(Vector3d, i8); +impl_commutative!(Vector3d, i16); +impl_commutative!(Vector3d, i32); +impl_commutative!(Vector3d, u8); +impl_commutative!(Vector3d, u16); +impl_commutative!(Vector3d, u32); +impl_commutative!(Vector3d, f32); +impl_commutative!(Vector3d, F32); + /// 3-dimensional vector #[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct Vector3d { @@ -207,6 +216,21 @@ where } } +impl Add for Vector3d +where + C: Component, +{ + type Output = Self; + + fn add(self, rhs: C) -> Self { + Self { + x: self.x + rhs, + y: self.y + rhs, + z: self.z + rhs, + } + } +} + impl Sub for Vector3d where C: Component, @@ -231,6 +255,21 @@ where } } +impl Sub for Vector3d +where + C: Component, +{ + type Output = Self; + + fn sub(self, rhs: C) -> Self { + Self { + x: self.x - rhs, + y: self.y - rhs, + z: self.z - rhs, + } + } +} + /// Compute the cross product of two vectors impl Mul for Vector3d where From ba740200cb107cd295975f2a1e856a8102930a01 Mon Sep 17 00:00:00 2001 From: Markus Mayer Date: Wed, 17 Jul 2024 17:03:06 +0200 Subject: [PATCH 2/2] Remove scalar Add and Sub for Vectors --- src/vector/commutative.rs | 8 -------- src/vector/vector2d.rs | 28 ---------------------------- src/vector/vector3d.rs | 30 ------------------------------ 3 files changed, 66 deletions(-) diff --git a/src/vector/commutative.rs b/src/vector/commutative.rs index 9080e1f..4aece40 100644 --- a/src/vector/commutative.rs +++ b/src/vector/commutative.rs @@ -1,14 +1,6 @@ /// Implements standard commutative operations such as Add and Mul on primitive types. macro_rules! impl_commutative { ($vector:ident, $component:ident) => { - impl Add<$vector<$component>> for $component { - type Output = $vector<$component>; - - fn add(self, rhs: $vector<$component>) -> Self::Output { - <$vector<$component> as Add<$component>>::add(rhs, self) - } - } - impl Mul<$vector<$component>> for $component { type Output = $vector<$component>; diff --git a/src/vector/vector2d.rs b/src/vector/vector2d.rs index 91acb92..64e81e1 100644 --- a/src/vector/vector2d.rs +++ b/src/vector/vector2d.rs @@ -205,20 +205,6 @@ where } } -impl Add for Vector2d -where - C: Component, -{ - type Output = Self; - - fn add(self, rhs: C) -> Self { - Self { - x: self.x + rhs, - y: self.y + rhs, - } - } -} - impl Sub for Vector2d where C: Component, @@ -242,20 +228,6 @@ where } } -impl Sub for Vector2d -where - C: Component, -{ - type Output = Self; - - fn sub(self, rhs: C) -> Self { - Self { - x: self.x - rhs, - y: self.y - rhs, - } - } -} - impl Mul for Vector2d where C: Component, diff --git a/src/vector/vector3d.rs b/src/vector/vector3d.rs index e6cc668..f89be1c 100644 --- a/src/vector/vector3d.rs +++ b/src/vector/vector3d.rs @@ -216,21 +216,6 @@ where } } -impl Add for Vector3d -where - C: Component, -{ - type Output = Self; - - fn add(self, rhs: C) -> Self { - Self { - x: self.x + rhs, - y: self.y + rhs, - z: self.z + rhs, - } - } -} - impl Sub for Vector3d where C: Component, @@ -255,21 +240,6 @@ where } } -impl Sub for Vector3d -where - C: Component, -{ - type Output = Self; - - fn sub(self, rhs: C) -> Self { - Self { - x: self.x - rhs, - y: self.y - rhs, - z: self.z - rhs, - } - } -} - /// Compute the cross product of two vectors impl Mul for Vector3d where