From 2ff660dfe1f8d0d168377c8a3951617b86ff2d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 13 Jul 2014 11:35:14 +0200 Subject: [PATCH] Add a `ScalarMul` and a `ScalarDiv` trait. Those might be useful to overcome rust limitations wrt operator overloading. --- src/na.rs | 1 + src/traits/mod.rs | 2 +- src/traits/operations.rs | 30 +++++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/na.rs b/src/na.rs index 3c03868e..d7a5bc28 100644 --- a/src/na.rs +++ b/src/na.rs @@ -33,6 +33,7 @@ pub use traits::{ Rotate, Rotation, RotationMatrix, RotationWithTranslation, Row, ScalarAdd, ScalarSub, + ScalarMul, ScalarAdd, ToHomogeneous, Transform, Transformation, Translate, Translation, diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 1c471b45..a4901797 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -9,7 +9,7 @@ pub use self::structure::{FloatVec, FloatVecExt, Basis, Cast, Col, Dim, Indexabl ColSlice, RowSlice, Eye}; pub use self::operations::{Absolute, ApproxEq, Cov, Inv, LMul, Mean, Outer, PartialOrd, RMul, - ScalarAdd, ScalarSub, Transpose}; + ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose}; pub use self::operations::{PartialOrdering, PartialLess, PartialEqual, PartialGreater, NotComparable}; pub mod geometry; diff --git a/src/traits/operations.rs b/src/traits/operations.rs index 382d3eb7..cb09a0ee 100644 --- a/src/traits/operations.rs +++ b/src/traits/operations.rs @@ -316,9 +316,7 @@ impl> ScalarAdd for T { } } -/** - * Trait of objects having a subtraction with a scalar. - */ +/// Trait of objects having a subtraction with a scalar. pub trait ScalarSub { /// Gets the result of `self - n`. fn sub_s(&self, n: &N) -> Self; @@ -330,3 +328,29 @@ impl> ScalarSub for T { *self - *n } } + +/// Trait of objects having a multiplication with a scalar. +pub trait ScalarMul { + /// Gets the result of `self * n`. + fn mul_s(&self, n: &N) -> Self; +} + +impl> ScalarMul for T { + /// Gets the result of `self * n`. + fn mul_s(&self, n: &N) -> T { + *self * *n + } +} + +/// Trait of objects having a division by a scalar. +pub trait ScalarDiv { + /// Gets the result of `self / n`. + fn div_s(&self, n: &N) -> Self; +} + +impl> ScalarDiv for T { + /// Gets the result of `self / n`. + fn div_s(&self, n: &N) -> T { + *self / *n + } +}