@@ -286,7 +286,16 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
286
286
/// as one would expect from a simple cast, but this behavior does not always make sense
287
287
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
288
288
#[ inline]
289
- pub fn cast < NewT : NumCast + Copy > ( & self ) -> Option < TypedPoint2D < NewT , U > > {
289
+ pub fn cast < NewT : NumCast + Copy > ( & self ) -> TypedPoint2D < NewT , U > {
290
+ self . try_cast ( ) . unwrap ( )
291
+ }
292
+
293
+ /// Fallible cast from one numeric representation to another, preserving the units.
294
+ ///
295
+ /// When casting from floating point to integer coordinates, the decimals are truncated
296
+ /// as one would expect from a simple cast, but this behavior does not always make sense
297
+ /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
298
+ pub fn try_cast < NewT : NumCast + Copy > ( & self ) -> Option < TypedPoint2D < NewT , U > > {
290
299
match ( NumCast :: from ( self . x ) , NumCast :: from ( self . y ) ) {
291
300
( Some ( x) , Some ( y) ) => Some ( point2 ( x, y) ) ,
292
301
_ => None ,
@@ -298,13 +307,13 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
298
307
/// Cast into an `f32` point.
299
308
#[ inline]
300
309
pub fn to_f32 ( & self ) -> TypedPoint2D < f32 , U > {
301
- self . cast ( ) . unwrap ( )
310
+ self . cast ( )
302
311
}
303
312
304
313
/// Cast into an `f64` point.
305
314
#[ inline]
306
315
pub fn to_f64 ( & self ) -> TypedPoint2D < f64 , U > {
307
- self . cast ( ) . unwrap ( )
316
+ self . cast ( )
308
317
}
309
318
310
319
/// Cast into an `usize` point, truncating decimals if any.
@@ -314,7 +323,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
314
323
/// the desired conversion behavior.
315
324
#[ inline]
316
325
pub fn to_usize ( & self ) -> TypedPoint2D < usize , U > {
317
- self . cast ( ) . unwrap ( )
326
+ self . cast ( )
318
327
}
319
328
320
329
/// Cast into an `u32` point, truncating decimals if any.
@@ -324,7 +333,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
324
333
/// the desired conversion behavior.
325
334
#[ inline]
326
335
pub fn to_u32 ( & self ) -> TypedPoint2D < u32 , U > {
327
- self . cast ( ) . unwrap ( )
336
+ self . cast ( )
328
337
}
329
338
330
339
/// Cast into an i32 point, truncating decimals if any.
@@ -334,7 +343,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
334
343
/// the desired conversion behavior.
335
344
#[ inline]
336
345
pub fn to_i32 ( & self ) -> TypedPoint2D < i32 , U > {
337
- self . cast ( ) . unwrap ( )
346
+ self . cast ( )
338
347
}
339
348
340
349
/// Cast into an i64 point, truncating decimals if any.
@@ -344,7 +353,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
344
353
/// the desired conversion behavior.
345
354
#[ inline]
346
355
pub fn to_i64 ( & self ) -> TypedPoint2D < i64 , U > {
347
- self . cast ( ) . unwrap ( )
356
+ self . cast ( )
348
357
}
349
358
}
350
359
@@ -650,7 +659,17 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
650
659
/// as one would expect from a simple cast, but this behavior does not always make sense
651
660
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
652
661
#[ inline]
653
- pub fn cast < NewT : NumCast + Copy > ( & self ) -> Option < TypedPoint3D < NewT , U > > {
662
+ pub fn cast < NewT : NumCast + Copy > ( & self ) -> TypedPoint3D < NewT , U > {
663
+ self . try_cast ( ) . unwrap ( )
664
+ }
665
+
666
+ /// Fallible cast from one numeric representation to another, preserving the units.
667
+ ///
668
+ /// When casting from floating point to integer coordinates, the decimals are truncated
669
+ /// as one would expect from a simple cast, but this behavior does not always make sense
670
+ /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
671
+ #[ inline]
672
+ pub fn try_cast < NewT : NumCast + Copy > ( & self ) -> Option < TypedPoint3D < NewT , U > > {
654
673
match (
655
674
NumCast :: from ( self . x ) ,
656
675
NumCast :: from ( self . y ) ,
@@ -666,13 +685,13 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
666
685
/// Cast into an `f32` point.
667
686
#[ inline]
668
687
pub fn to_f32 ( & self ) -> TypedPoint3D < f32 , U > {
669
- self . cast ( ) . unwrap ( )
688
+ self . cast ( )
670
689
}
671
690
672
691
/// Cast into an `f64` point.
673
692
#[ inline]
674
693
pub fn to_f64 ( & self ) -> TypedPoint3D < f64 , U > {
675
- self . cast ( ) . unwrap ( )
694
+ self . cast ( )
676
695
}
677
696
678
697
/// Cast into an `usize` point, truncating decimals if any.
@@ -682,7 +701,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
682
701
/// the desired conversion behavior.
683
702
#[ inline]
684
703
pub fn to_usize ( & self ) -> TypedPoint3D < usize , U > {
685
- self . cast ( ) . unwrap ( )
704
+ self . cast ( )
686
705
}
687
706
688
707
/// Cast into an `u32` point, truncating decimals if any.
@@ -692,7 +711,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
692
711
/// the desired conversion behavior.
693
712
#[ inline]
694
713
pub fn to_u32 ( & self ) -> TypedPoint3D < u32 , U > {
695
- self . cast ( ) . unwrap ( )
714
+ self . cast ( )
696
715
}
697
716
698
717
/// Cast into an `i32` point, truncating decimals if any.
@@ -702,7 +721,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
702
721
/// the desired conversion behavior.
703
722
#[ inline]
704
723
pub fn to_i32 ( & self ) -> TypedPoint3D < i32 , U > {
705
- self . cast ( ) . unwrap ( )
724
+ self . cast ( )
706
725
}
707
726
708
727
/// Cast into an `i64` point, truncating decimals if any.
@@ -712,7 +731,7 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
712
731
/// the desired conversion behavior.
713
732
#[ inline]
714
733
pub fn to_i64 ( & self ) -> TypedPoint3D < i64 , U > {
715
- self . cast ( ) . unwrap ( )
734
+ self . cast ( )
716
735
}
717
736
}
718
737
0 commit comments