Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commutative multiplication on primitive scalar types for Vectors #116

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
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::FromIterator,
Expand Down Expand Up @@ -28,6 +28,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
Loading