diff --git a/src/quaternion.rs b/src/quaternion.rs index 599756d..b3edec5 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -18,7 +18,7 @@ use std::mem; use std::ops::*; use rand::{Rand, Rng}; -use num_traits::cast; +use num_traits::{NumCast, cast}; use structure::*; @@ -229,6 +229,13 @@ impl MetricSpace for Quaternion { } } +impl Quaternion { + /// Component-wise casting to another type. + pub fn cast(&self) -> Quaternion { + Quaternion::from_sv(NumCast::from(self.s).unwrap(), self.v.cast()) + } +} + #[cfg(not(feature = "simd"))] impl InnerSpace for Quaternion { #[inline] diff --git a/tests/quaternion.rs b/tests/quaternion.rs index 89bd531..315c5f3 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -331,3 +331,13 @@ mod rotate_between_vectors { assert_ulps_eq!(Quaternion::between_vectors(a, b), expected); } } + +mod cast { + use cgmath::*; + + #[test] + fn test_cast() { + assert_ulps_eq!(Quaternion::new(0.9f64, 1.5, 2.4, 7.6).cast(), + Quaternion::new(0.9f32, 1.5, 2.4, 7.6)); + } +}