From a6abd5bf02eac60352ffb3e8019486317c16c67b Mon Sep 17 00:00:00 2001 From: Osspial Date: Wed, 16 Aug 2017 16:21:51 -0400 Subject: [PATCH] Modify cast() functions to return Option --- src/matrix.rs | 10 ++++++++-- src/point.rs | 10 ++++++++-- src/quaternion.rs | 12 ++++++++++-- src/vector.rs | 10 ++++++++-- tests/matrix.rs | 6 +++--- tests/point.rs | 6 +++--- tests/quaternion.rs | 2 +- tests/vector.rs | 6 +++--- tests/vector4f32.rs | 2 +- 9 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 176a954..2b488b1 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1025,8 +1025,14 @@ macro_rules! impl_matrix { impl $MatrixN { /// Component-wise casting to another type #[inline] - pub fn cast(&self) -> $MatrixN { - $MatrixN { $($field: self.$field.cast() ),+ } + pub fn cast(&self) -> Option<$MatrixN> { + $( + let $field = match self.$field.cast() { + Some(field) => field, + None => return None + }; + )+ + Some($MatrixN { $($field),+ }) } } } diff --git a/src/point.rs b/src/point.rs index 1533321..35550e3 100644 --- a/src/point.rs +++ b/src/point.rs @@ -127,8 +127,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()),+ } + pub fn cast(&self) -> Option<$PointN> { + $( + let $field = match NumCast::from(self.$field) { + Some(field) => field, + None => return None + }; + )+ + Some($PointN { $($field),+ }) } } diff --git a/src/quaternion.rs b/src/quaternion.rs index 5063d52..d9eeae2 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -234,8 +234,16 @@ 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()) + pub fn cast(&self) -> Option> { + let s = match NumCast::from(self.s) { + Some(s) => s, + None => return None + }; + let v = match self.v.cast() { + Some(v) => v, + None => return None + }; + Some(Quaternion::from_sv(s, v)) } } diff --git a/src/vector.rs b/src/vector.rs index 70dd081..3a48775 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -112,8 +112,14 @@ macro_rules! impl_vector { impl $VectorN { /// Component-wise casting to another type. #[inline] - pub fn cast(&self) -> $VectorN { - $VectorN { $($field: NumCast::from(self.$field).unwrap()),+ } + pub fn cast(&self) -> Option<$VectorN> { + $( + let $field = match NumCast::from(self.$field) { + Some(field) => field, + None => return None + }; + )+ + Some($VectorN { $($field),+ }) } } diff --git a/tests/matrix.rs b/tests/matrix.rs index 74fb101..80f1152 100644 --- a/tests/matrix.rs +++ b/tests/matrix.rs @@ -741,12 +741,12 @@ pub mod matrix4 { #[test] fn test_cast() { - assert_ulps_eq!(Matrix2::new(0.2f64, 1.5, 4.7, 2.3).cast(), Matrix2::new(0.2f32, 1.5, 4.7, 2.3)); + assert_ulps_eq!(Matrix2::new(0.2f64, 1.5, 4.7, 2.3).cast().unwrap(), Matrix2::new(0.2f32, 1.5, 4.7, 2.3)); assert_ulps_eq!(Matrix3::new( 0.2f64, 1.5, 4.7, 2.3, 5.7, 2.1, 4.6, 5.2, 6.6, - ).cast(), Matrix3::new( + ).cast().unwrap(), Matrix3::new( 0.2f32, 1.5, 4.7, 2.3, 5.7, 2.1, 4.6, 5.2, 6.6, @@ -757,7 +757,7 @@ pub mod matrix4 { 2.3, 5.7, 2.1, 1.1, 4.6, 5.2, 6.6, 0.2, 3.2, 1.8, 0.4, 2.9, - ).cast(), Matrix4::new( + ).cast().unwrap(), Matrix4::new( 0.2f32, 1.5, 4.7, 2.5, 2.3, 5.7, 2.1, 1.1, 4.6, 5.2, 6.6, 0.2, diff --git a/tests/point.rs b/tests/point.rs index 0078189..0f3fc8a 100644 --- a/tests/point.rs +++ b/tests/point.rs @@ -77,7 +77,7 @@ fn test_rem() { #[test] fn test_cast() { - assert_ulps_eq!(Point1::new(0.9f64).cast(), Point1::new(0.9f32)); - assert_ulps_eq!(Point2::new(0.9f64, 1.5).cast(), Point2::new(0.9f32, 1.5)); - assert_ulps_eq!(Point3::new(1.0f64, 2.4, -3.13).cast(), Point3::new(1.0f32, 2.4, -3.13)); + assert_ulps_eq!(Point1::new(0.9f64).cast().unwrap(), Point1::new(0.9f32)); + assert_ulps_eq!(Point2::new(0.9f64, 1.5).cast().unwrap(), Point2::new(0.9f32, 1.5)); + assert_ulps_eq!(Point3::new(1.0f64, 2.4, -3.13).cast().unwrap(), Point3::new(1.0f32, 2.4, -3.13)); } diff --git a/tests/quaternion.rs b/tests/quaternion.rs index 315c5f3..2496935 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -337,7 +337,7 @@ mod cast { #[test] fn test_cast() { - assert_ulps_eq!(Quaternion::new(0.9f64, 1.5, 2.4, 7.6).cast(), + assert_ulps_eq!(Quaternion::new(0.9f64, 1.5, 2.4, 7.6).cast().unwrap(), Quaternion::new(0.9f32, 1.5, 2.4, 7.6)); } } diff --git a/tests/vector.rs b/tests/vector.rs index 7cb799b..317391d 100644 --- a/tests/vector.rs +++ b/tests/vector.rs @@ -250,7 +250,7 @@ fn test_normalize() { #[test] fn test_cast() { - assert_ulps_eq!(Vector2::new(0.9f64, 1.5).cast(), Vector2::new(0.9f32, 1.5)); - assert_ulps_eq!(Vector3::new(1.0f64, 2.4, -3.13).cast(), Vector3::new(1.0f32, 2.4, -3.13)); - assert_ulps_eq!(Vector4::new(13.5f64, -4.6, -8.3, 2.41).cast(), Vector4::new(13.5f32, -4.6, -8.3, 2.41)); + assert_ulps_eq!(Vector2::new(0.9f64, 1.5).cast().unwrap(), Vector2::new(0.9f32, 1.5)); + assert_ulps_eq!(Vector3::new(1.0f64, 2.4, -3.13).cast().unwrap(), Vector3::new(1.0f32, 2.4, -3.13)); + assert_ulps_eq!(Vector4::new(13.5f64, -4.6, -8.3, 2.41).cast().unwrap(), Vector4::new(13.5f32, -4.6, -8.3, 2.41)); } diff --git a/tests/vector4f32.rs b/tests/vector4f32.rs index 28b07f3..6889471 100644 --- a/tests/vector4f32.rs +++ b/tests/vector4f32.rs @@ -171,5 +171,5 @@ fn test_normalize() { #[test] fn test_cast() { - assert_ulps_eq!(Vector4::new(13.5f32, -4.6, -8.3, 2.41).cast(), Vector4::new(13.5f32, -4.6, -8.3, 2.41)); + assert_ulps_eq!(Vector4::new(13.5f32, -4.6, -8.3, 2.41).cast().unwrap(), Vector4::new(13.5f32, -4.6, -8.3, 2.41)); }