Add a ScalarMul and a ScalarDiv trait.

Those might be useful to overcome rust limitations wrt operator overloading.
This commit is contained in:
Sébastien Crozet 2014-07-13 11:35:14 +02:00
parent 5929640883
commit 2ff660dfe1
3 changed files with 29 additions and 4 deletions

View File

@ -33,6 +33,7 @@ pub use traits::{
Rotate, Rotation, RotationMatrix, RotationWithTranslation, Rotate, Rotation, RotationMatrix, RotationWithTranslation,
Row, Row,
ScalarAdd, ScalarSub, ScalarAdd, ScalarSub,
ScalarMul, ScalarAdd,
ToHomogeneous, ToHomogeneous,
Transform, Transformation, Transform, Transformation,
Translate, Translation, Translate, Translation,

View File

@ -9,7 +9,7 @@ pub use self::structure::{FloatVec, FloatVecExt, Basis, Cast, Col, Dim, Indexabl
ColSlice, RowSlice, Eye}; ColSlice, RowSlice, Eye};
pub use self::operations::{Absolute, ApproxEq, Cov, Inv, LMul, Mean, Outer, PartialOrd, RMul, 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 use self::operations::{PartialOrdering, PartialLess, PartialEqual, PartialGreater, NotComparable};
pub mod geometry; pub mod geometry;

View File

@ -316,9 +316,7 @@ impl<N, T: Add<N, T>> ScalarAdd<N> for T {
} }
} }
/** /// Trait of objects having a subtraction with a scalar.
* Trait of objects having a subtraction with a scalar.
*/
pub trait ScalarSub<N> { pub trait ScalarSub<N> {
/// Gets the result of `self - n`. /// Gets the result of `self - n`.
fn sub_s(&self, n: &N) -> Self; fn sub_s(&self, n: &N) -> Self;
@ -330,3 +328,29 @@ impl<N, T: Sub<N, T>> ScalarSub<N> for T {
*self - *n *self - *n
} }
} }
/// Trait of objects having a multiplication with a scalar.
pub trait ScalarMul<N> {
/// Gets the result of `self * n`.
fn mul_s(&self, n: &N) -> Self;
}
impl<N, T: Mul<N, T>> ScalarMul<N> 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<N> {
/// Gets the result of `self / n`.
fn div_s(&self, n: &N) -> Self;
}
impl<N, T: Div<N, T>> ScalarDiv<N> for T {
/// Gets the result of `self / n`.
fn div_s(&self, n: &N) -> T {
*self / *n
}
}