diff --git a/src/vector.rs b/src/vector.rs index adc4b46..cf94765 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..4aece40 --- /dev/null +++ b/src/vector/commutative.rs @@ -0,0 +1,14 @@ +/// Implements standard commutative operations such as Add and Mul on primitive types. +macro_rules! impl_commutative { + ($vector:ident, $component:ident) => { + 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 99f980b..dc0d99b 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, Sum}, 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 { diff --git a/src/vector/vector3d.rs b/src/vector/vector3d.rs index a9f054a..32c6ea4 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::Sum; use core::{ @@ -29,6 +29,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 {