Skip to content

Commit

Permalink
Add commutative multiplication on primitive scalar types for Vectors (#…
Browse files Browse the repository at this point in the history
…116)

Addresses #105
  • Loading branch information
sunsided authored Sep 5, 2024
1 parent f96f696 commit 8533f2d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/vector/commutative.rs
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions src/vector/vector2d.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -27,6 +29,15 @@ pub type U32x2 = Vector2d<u32>;
/// 2-dimensional XY vector of `f32` values
pub type F32x2 = Vector2d<f32>;

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<C: Component> {
Expand Down
11 changes: 10 additions & 1 deletion src/vector/vector3d.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -29,6 +29,15 @@ pub type U32x3 = Vector3d<u32>;
/// 3-dimensional XYZ vector of `f32` values
pub type F32x3 = Vector3d<f32>;

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<C: Component> {
Expand Down

0 comments on commit 8533f2d

Please sign in to comment.