diff --git a/src/matrix.rs b/src/matrix.rs index 9d3acdf..6a1e2fd 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -14,7 +14,7 @@ // limitations under the License. use rand::{Rand, Rng}; -use num_traits::cast; +use num_traits::{cast, NumCast}; use std::fmt; use std::mem; use std::ops::*; @@ -917,7 +917,7 @@ impl Transform3 for Matrix3 {} impl Transform3 for Matrix4 {} -macro_rules! impl_operators { +macro_rules! impl_matrix { ($MatrixN:ident, $VectorN:ident { $($field:ident : $row_index:expr),+ }) => { impl_operator!( Neg for $MatrixN { fn neg(matrix) -> $MatrixN { $MatrixN { $($field: -matrix.$field),+ } } @@ -971,6 +971,15 @@ macro_rules! impl_operators { impl_scalar_ops!($MatrixN { $($field),+ }); impl_scalar_ops!($MatrixN { $($field),+ }); impl_scalar_ops!($MatrixN { $($field),+ }); + + + impl $MatrixN { + /// Component-wise casting to another type + #[inline] + pub fn cast(&self) -> $MatrixN { + $MatrixN { $($field: self.$field.cast() ),+ } + } + } } } @@ -988,9 +997,9 @@ macro_rules! impl_scalar_ops { }; } -impl_operators!(Matrix2, Vector2 { x: 0, y: 1 }); -impl_operators!(Matrix3, Vector3 { x: 0, y: 1, z: 2 }); -impl_operators!(Matrix4, Vector4 { x: 0, y: 1, z: 2, w: 3 }); +impl_matrix!(Matrix2, Vector2 { x: 0, y: 1 }); +impl_matrix!(Matrix3, Vector3 { x: 0, y: 1, z: 2 }); +impl_matrix!(Matrix4, Vector4 { x: 0, y: 1, z: 2, w: 3 }); impl_operator!( Mul > for Matrix2 { fn mul(lhs, rhs) -> Matrix2 { diff --git a/src/point.rs b/src/point.rs index a81312c..92ab046 100644 --- a/src/point.rs +++ b/src/point.rs @@ -17,6 +17,7 @@ //! disinguishes them from vectors, which have a length and direction, but do //! not have a fixed position. +use num_traits::NumCast; use std::fmt; use std::mem; use std::ops::*; @@ -128,6 +129,14 @@ macro_rules! impl_point { } } + impl $PointN { + /// Component-wise casting to another type + #[inline] + pub fn cast(&self) -> $PointN { + $PointN { $($field: NumCast::from(self.$field).unwrap()),+ } + } + } + impl MetricSpace for $PointN { type Metric = S;