Skip to content

Commit

Permalink
Auto merge of #294 - nical:try-cst, r=kvark
Browse files Browse the repository at this point in the history
Make cast infallible and add try_cast.

Fixes #270.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/euclid/294)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Jun 14, 2018
2 parents 54a98c0 + ee8d415 commit 766d2e9
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 51 deletions.
9 changes: 7 additions & 2 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ impl<U, T: Clone + Neg<Output = T>> Neg for Length<T, U> {

impl<Unit, T0: NumCast + Clone> Length<T0, Unit> {
/// Cast from one numeric representation to another, preserving the units.
pub fn cast<T1: NumCast + Clone>(&self) -> Option<Length<T1, Unit>> {
pub fn cast<T1: NumCast + Clone>(&self) -> Length<T1, Unit> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
pub fn try_cast<T1: NumCast + Clone>(&self) -> Option<Length<T1, Unit>> {
NumCast::from(self.get()).map(Length::new)
}
}
Expand Down Expand Up @@ -461,7 +466,7 @@ mod tests {
fn test_cast() {
let length_as_i32: Length<i32, Cm> = Length::new(5);

let result: Length<f32, Cm> = length_as_i32.cast().unwrap();
let result: Length<f32, Cm> = length_as_i32.cast();

let length_as_f32: Length<f32, Cm> = Length::new(5.0);
assert_eq!(result, length_as_f32);
Expand Down
47 changes: 33 additions & 14 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
#[inline]
pub fn cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint2D<NewT, U>> {
pub fn cast<NewT: NumCast + Copy>(&self) -> TypedPoint2D<NewT, U> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
pub fn try_cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint2D<NewT, U>> {
match (NumCast::from(self.x), NumCast::from(self.y)) {
(Some(x), Some(y)) => Some(point2(x, y)),
_ => None,
Expand All @@ -298,13 +307,13 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// Cast into an `f32` point.
#[inline]
pub fn to_f32(&self) -> TypedPoint2D<f32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `f64` point.
#[inline]
pub fn to_f64(&self) -> TypedPoint2D<f64, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `usize` point, truncating decimals if any.
Expand All @@ -314,7 +323,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_usize(&self) -> TypedPoint2D<usize, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `u32` point, truncating decimals if any.
Expand All @@ -324,7 +333,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_u32(&self) -> TypedPoint2D<u32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an i32 point, truncating decimals if any.
Expand All @@ -334,7 +343,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_i32(&self) -> TypedPoint2D<i32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an i64 point, truncating decimals if any.
Expand All @@ -344,7 +353,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_i64(&self) -> TypedPoint2D<i64, U> {
self.cast().unwrap()
self.cast()
}
}

Expand Down Expand Up @@ -650,7 +659,17 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
#[inline]
pub fn cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint3D<NewT, U>> {
pub fn cast<NewT: NumCast + Copy>(&self) -> TypedPoint3D<NewT, U> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
#[inline]
pub fn try_cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint3D<NewT, U>> {
match (
NumCast::from(self.x),
NumCast::from(self.y),
Expand All @@ -666,13 +685,13 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// Cast into an `f32` point.
#[inline]
pub fn to_f32(&self) -> TypedPoint3D<f32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `f64` point.
#[inline]
pub fn to_f64(&self) -> TypedPoint3D<f64, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `usize` point, truncating decimals if any.
Expand All @@ -682,7 +701,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_usize(&self) -> TypedPoint3D<usize, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `u32` point, truncating decimals if any.
Expand All @@ -692,7 +711,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_u32(&self) -> TypedPoint3D<u32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i32` point, truncating decimals if any.
Expand All @@ -702,7 +721,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_i32(&self) -> TypedPoint3D<i32, U> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i64` point, truncating decimals if any.
Expand All @@ -712,7 +731,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
/// the desired conversion behavior.
#[inline]
pub fn to_i64(&self) -> TypedPoint3D<i64, U> {
self.cast().unwrap()
self.cast()
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,20 @@ impl<T0: NumCast + Copy, Unit> TypedRect<T0, Unit> {
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using round(), round_in or round_out() before casting.
pub fn cast<T1: NumCast + Copy>(&self) -> Option<TypedRect<T1, Unit>> {
match (self.origin.cast(), self.size.cast()) {
pub fn cast<T1: NumCast + Copy>(&self) -> TypedRect<T1, Unit> {
TypedRect::new(
self.origin.cast(),
self.size.cast(),
)
}

/// Fallible cast from one numeric representation to another, preserving the units.
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using round(), round_in or round_out() before casting.
pub fn try_cast<T1: NumCast + Copy>(&self) -> Option<TypedRect<T1, Unit>> {
match (self.origin.try_cast(), self.size.try_cast()) {
(Some(origin), Some(size)) => Some(TypedRect::new(origin, size)),
_ => None,
}
Expand Down Expand Up @@ -518,12 +530,12 @@ impl<T: Floor + Ceil + Round + Add<T, Output = T> + Sub<T, Output = T>, U> Typed
impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// Cast into an `f32` rectangle.
pub fn to_f32(&self) -> TypedRect<f32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `f64` rectangle.
pub fn to_f64(&self) -> TypedRect<f64, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `usize` rectangle, truncating decimals if any.
Expand All @@ -532,7 +544,7 @@ impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_usize(&self) -> TypedRect<usize, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `u32` rectangle, truncating decimals if any.
Expand All @@ -541,7 +553,7 @@ impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_u32(&self) -> TypedRect<u32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i32` rectangle, truncating decimals if any.
Expand All @@ -550,7 +562,7 @@ impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_i32(&self) -> TypedRect<i32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i64` rectangle, truncating decimals if any.
Expand All @@ -559,7 +571,7 @@ impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_i64(&self) -> TypedRect<i64, Unit> {
self.cast().unwrap()
self.cast()
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ impl<T: Clone + Sub<T, Output = T>, Src, Dst> Sub for TypedScale<T, Src, Dst> {

impl<T: NumCast + Clone, Src, Dst0> TypedScale<T, Src, Dst0> {
/// Cast from one numeric representation to another, preserving the units.
pub fn cast<T1: NumCast + Clone>(&self) -> Option<TypedScale<T1, Src, Dst0>> {
pub fn cast<T1: NumCast + Clone>(&self) -> TypedScale<T1, Src, Dst0> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
pub fn try_cast<T1: NumCast + Clone>(&self) -> Option<TypedScale<T1, Src, Dst0>> {
NumCast::from(self.get()).map(TypedScale::new)
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,16 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
pub fn cast<NewT: NumCast + Copy>(&self) -> Option<TypedSize2D<NewT, Unit>> {
pub fn cast<NewT: NumCast + Copy>(&self) -> TypedSize2D<NewT, Unit> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
pub fn try_cast<NewT: NumCast + Copy>(&self) -> Option<TypedSize2D<NewT, Unit>> {
match (NumCast::from(self.width), NumCast::from(self.height)) {
(Some(w), Some(h)) => Some(TypedSize2D::new(w, h)),
_ => None,
Expand All @@ -227,12 +236,12 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {

/// Cast into an `f32` size.
pub fn to_f32(&self) -> TypedSize2D<f32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `f64` size.
pub fn to_f64(&self) -> TypedSize2D<f64, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `uint` size, truncating decimals if any.
Expand All @@ -241,7 +250,7 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_usize(&self) -> TypedSize2D<usize, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `u32` size, truncating decimals if any.
Expand All @@ -250,7 +259,7 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_u32(&self) -> TypedSize2D<u32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i32` size, truncating decimals if any.
Expand All @@ -259,7 +268,7 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i32(&self) -> TypedSize2D<i32, Unit> {
self.cast().unwrap()
self.cast()
}

/// Cast into an `i64` size, truncating decimals if any.
Expand All @@ -268,7 +277,7 @@ impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i64(&self) -> TypedSize2D<i64, Unit> {
self.cast().unwrap()
self.cast()
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,23 @@ impl<T: Copy, Src, Dst> TypedTransform2D<T, Src, Dst> {

impl<T0: NumCast + Copy, Src, Dst> TypedTransform2D<T0, Src, Dst> {
/// Cast from one numeric representation to another, preserving the units.
pub fn cast<T1: NumCast + Copy>(&self) -> Option<TypedTransform2D<T1, Src, Dst>> {
pub fn cast<T1: NumCast + Copy>(&self) -> TypedTransform2D<T1, Src, Dst> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
pub fn try_cast<T1: NumCast + Copy>(&self) -> Option<TypedTransform2D<T1, Src, Dst>> {
match (NumCast::from(self.m11), NumCast::from(self.m12),
NumCast::from(self.m21), NumCast::from(self.m22),
NumCast::from(self.m31), NumCast::from(self.m32)) {
(Some(m11), Some(m12),
Some(m21), Some(m22),
Some(m31), Some(m32)) => {
Some(TypedTransform2D::row_major(m11, m12,
m21, m22,
m31, m32))
Some(TypedTransform2D::row_major(
m11, m12,
m21, m22,
m31, m32
))
},
_ => None
}
Expand Down
7 changes: 6 additions & 1 deletion src/transform3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,12 @@ impl<T: Copy, Src, Dst> TypedTransform3D<T, Src, Dst> {

impl<T0: NumCast + Copy, Src, Dst> TypedTransform3D<T0, Src, Dst> {
/// Cast from one numeric representation to another, preserving the units.
pub fn cast<T1: NumCast + Copy>(&self) -> Option<TypedTransform3D<T1, Src, Dst>> {
pub fn cast<T1: NumCast + Copy>(&self) -> TypedTransform3D<T1, Src, Dst> {
self.try_cast().unwrap()
}

/// Fallible cast from one numeric representation to another, preserving the units.
pub fn try_cast<T1: NumCast + Copy>(&self) -> Option<TypedTransform3D<T1, Src, Dst>> {
match (NumCast::from(self.m11), NumCast::from(self.m12),
NumCast::from(self.m13), NumCast::from(self.m14),
NumCast::from(self.m21), NumCast::from(self.m22),
Expand Down
Loading

0 comments on commit 766d2e9

Please sign in to comment.