Modify cast() functions to return Option
This commit is contained in:
parent
7823f46b68
commit
a6abd5bf02
9 changed files with 45 additions and 19 deletions
|
@ -1025,8 +1025,14 @@ macro_rules! impl_matrix {
|
||||||
impl<S: NumCast + Copy> $MatrixN<S> {
|
impl<S: NumCast + Copy> $MatrixN<S> {
|
||||||
/// Component-wise casting to another type
|
/// Component-wise casting to another type
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cast<T: NumCast>(&self) -> $MatrixN<T> {
|
pub fn cast<T: NumCast>(&self) -> Option<$MatrixN<T>> {
|
||||||
$MatrixN { $($field: self.$field.cast() ),+ }
|
$(
|
||||||
|
let $field = match self.$field.cast() {
|
||||||
|
Some(field) => field,
|
||||||
|
None => return None
|
||||||
|
};
|
||||||
|
)+
|
||||||
|
Some($MatrixN { $($field),+ })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/point.rs
10
src/point.rs
|
@ -127,8 +127,14 @@ macro_rules! impl_point {
|
||||||
impl<S: NumCast + Copy> $PointN<S> {
|
impl<S: NumCast + Copy> $PointN<S> {
|
||||||
/// Component-wise casting to another type
|
/// Component-wise casting to another type
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cast<T: NumCast>(&self) -> $PointN<T> {
|
pub fn cast<T: NumCast>(&self) -> Option<$PointN<T>> {
|
||||||
$PointN { $($field: NumCast::from(self.$field).unwrap()),+ }
|
$(
|
||||||
|
let $field = match NumCast::from(self.$field) {
|
||||||
|
Some(field) => field,
|
||||||
|
None => return None
|
||||||
|
};
|
||||||
|
)+
|
||||||
|
Some($PointN { $($field),+ })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -234,8 +234,16 @@ impl<S: BaseFloat> MetricSpace for Quaternion<S> {
|
||||||
|
|
||||||
impl<S: NumCast + Copy> Quaternion<S> {
|
impl<S: NumCast + Copy> Quaternion<S> {
|
||||||
/// Component-wise casting to another type.
|
/// Component-wise casting to another type.
|
||||||
pub fn cast<T: BaseFloat>(&self) -> Quaternion<T> {
|
pub fn cast<T: BaseFloat>(&self) -> Option<Quaternion<T>> {
|
||||||
Quaternion::from_sv(NumCast::from(self.s).unwrap(), self.v.cast())
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,8 +112,14 @@ macro_rules! impl_vector {
|
||||||
impl<S: NumCast + Copy> $VectorN<S> {
|
impl<S: NumCast + Copy> $VectorN<S> {
|
||||||
/// Component-wise casting to another type.
|
/// Component-wise casting to another type.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cast<T: NumCast>(&self) -> $VectorN<T> {
|
pub fn cast<T: NumCast>(&self) -> Option<$VectorN<T>> {
|
||||||
$VectorN { $($field: NumCast::from(self.$field).unwrap()),+ }
|
$(
|
||||||
|
let $field = match NumCast::from(self.$field) {
|
||||||
|
Some(field) => field,
|
||||||
|
None => return None
|
||||||
|
};
|
||||||
|
)+
|
||||||
|
Some($VectorN { $($field),+ })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -741,12 +741,12 @@ pub mod matrix4 {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cast() {
|
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(
|
assert_ulps_eq!(Matrix3::new(
|
||||||
0.2f64, 1.5, 4.7,
|
0.2f64, 1.5, 4.7,
|
||||||
2.3, 5.7, 2.1,
|
2.3, 5.7, 2.1,
|
||||||
4.6, 5.2, 6.6,
|
4.6, 5.2, 6.6,
|
||||||
).cast(), Matrix3::new(
|
).cast().unwrap(), Matrix3::new(
|
||||||
0.2f32, 1.5, 4.7,
|
0.2f32, 1.5, 4.7,
|
||||||
2.3, 5.7, 2.1,
|
2.3, 5.7, 2.1,
|
||||||
4.6, 5.2, 6.6,
|
4.6, 5.2, 6.6,
|
||||||
|
@ -757,7 +757,7 @@ pub mod matrix4 {
|
||||||
2.3, 5.7, 2.1, 1.1,
|
2.3, 5.7, 2.1, 1.1,
|
||||||
4.6, 5.2, 6.6, 0.2,
|
4.6, 5.2, 6.6, 0.2,
|
||||||
3.2, 1.8, 0.4, 2.9,
|
3.2, 1.8, 0.4, 2.9,
|
||||||
).cast(), Matrix4::new(
|
).cast().unwrap(), Matrix4::new(
|
||||||
0.2f32, 1.5, 4.7, 2.5,
|
0.2f32, 1.5, 4.7, 2.5,
|
||||||
2.3, 5.7, 2.1, 1.1,
|
2.3, 5.7, 2.1, 1.1,
|
||||||
4.6, 5.2, 6.6, 0.2,
|
4.6, 5.2, 6.6, 0.2,
|
||||||
|
|
|
@ -77,7 +77,7 @@ fn test_rem() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cast() {
|
fn test_cast() {
|
||||||
assert_ulps_eq!(Point1::new(0.9f64).cast(), Point1::new(0.9f32));
|
assert_ulps_eq!(Point1::new(0.9f64).cast().unwrap(), Point1::new(0.9f32));
|
||||||
assert_ulps_eq!(Point2::new(0.9f64, 1.5).cast(), Point2::new(0.9f32, 1.5));
|
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(), Point3::new(1.0f32, 2.4, -3.13));
|
assert_ulps_eq!(Point3::new(1.0f64, 2.4, -3.13).cast().unwrap(), Point3::new(1.0f32, 2.4, -3.13));
|
||||||
}
|
}
|
||||||
|
|
|
@ -337,7 +337,7 @@ mod cast {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cast() {
|
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));
|
Quaternion::new(0.9f32, 1.5, 2.4, 7.6));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -250,7 +250,7 @@ fn test_normalize() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cast() {
|
fn test_cast() {
|
||||||
assert_ulps_eq!(Vector2::new(0.9f64, 1.5).cast(), Vector2::new(0.9f32, 1.5));
|
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(), Vector3::new(1.0f32, 2.4, -3.13));
|
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(), Vector4::new(13.5f32, -4.6, -8.3, 2.41));
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,5 +171,5 @@ fn test_normalize() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cast() {
|
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));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue