@@ -679,21 +679,9 @@ impl Duration {
679
679
#[ inline]
680
680
#[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
681
681
pub const fn from_secs_f64 ( secs : f64 ) -> Duration {
682
- const MAX_NANOS_F64 : f64 = ( ( u64:: MAX as u128 + 1 ) * ( NANOS_PER_SEC as u128 ) ) as f64 ;
683
- let nanos = secs * ( NANOS_PER_SEC as f64 ) ;
684
- if !nanos. is_finite ( ) {
685
- panic ! ( "got non-finite value when converting float to duration" ) ;
686
- }
687
- if nanos >= MAX_NANOS_F64 {
688
- panic ! ( "overflow when converting float to duration" ) ;
689
- }
690
- if nanos < 0.0 {
691
- panic ! ( "underflow when converting float to duration" ) ;
692
- }
693
- let nanos = nanos as u128 ;
694
- Duration {
695
- secs : ( nanos / ( NANOS_PER_SEC as u128 ) ) as u64 ,
696
- nanos : ( nanos % ( NANOS_PER_SEC as u128 ) ) as u32 ,
682
+ match Duration :: try_from_secs_f64 ( secs) {
683
+ Ok ( v) => v,
684
+ Err ( e) => crate :: panicking:: panic ( e. description ( ) ) ,
697
685
}
698
686
}
699
687
@@ -752,21 +740,9 @@ impl Duration {
752
740
#[ inline]
753
741
#[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
754
742
pub const fn from_secs_f32 ( secs : f32 ) -> Duration {
755
- const MAX_NANOS_F32 : f32 = ( ( u64:: MAX as u128 + 1 ) * ( NANOS_PER_SEC as u128 ) ) as f32 ;
756
- let nanos = secs * ( NANOS_PER_SEC as f32 ) ;
757
- if !nanos. is_finite ( ) {
758
- panic ! ( "got non-finite value when converting float to duration" ) ;
759
- }
760
- if nanos >= MAX_NANOS_F32 {
761
- panic ! ( "overflow when converting float to duration" ) ;
762
- }
763
- if nanos < 0.0 {
764
- panic ! ( "underflow when converting float to duration" ) ;
765
- }
766
- let nanos = nanos as u128 ;
767
- Duration {
768
- secs : ( nanos / ( NANOS_PER_SEC as u128 ) ) as u64 ,
769
- nanos : ( nanos % ( NANOS_PER_SEC as u128 ) ) as u32 ,
743
+ match Duration :: try_from_secs_f32 ( secs) {
744
+ Ok ( v) => v,
745
+ Err ( e) => crate :: panicking:: panic ( e. description ( ) ) ,
770
746
}
771
747
}
772
748
0 commit comments