@@ -30,7 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
30
30
const NANOS_PER_MICRO : u32 = 1_000 ;
31
31
const MILLIS_PER_SEC : u64 = 1_000 ;
32
32
const MICROS_PER_SEC : u64 = 1_000_000 ;
33
- const MAX_NANOS_F64 : f64 = ( ( u64:: MAX as u128 ) * ( NANOS_PER_SEC as u128 ) ) as f64 ;
33
+ const MAX_NANOS_F64 : f64 = ( ( u64:: MAX as u128 + 1 ) * ( NANOS_PER_SEC as u128 ) - 1 ) as f64 ;
34
34
35
35
/// A `Duration` type to represent a span of time, typically used for system
36
36
/// timeouts.
@@ -491,7 +491,17 @@ impl Duration {
491
491
#[ unstable( feature = "duration_float" , issue = "0" ) ]
492
492
#[ inline]
493
493
pub fn from_float_secs ( secs : f64 ) -> Duration {
494
- let nanos = ( secs * ( NANOS_PER_SEC as f64 ) ) as u128 ;
494
+ let nanos = secs * ( NANOS_PER_SEC as f64 ) ;
495
+ if !nanos. is_finite ( ) {
496
+ panic ! ( "got non-finite value when converting float to duration" ) ;
497
+ }
498
+ if nanos > MAX_NANOS_F64 {
499
+ panic ! ( "overflow when converting float to duration" ) ;
500
+ }
501
+ if nanos < 0.0 {
502
+ panic ! ( "underflow when converting float to duration" ) ;
503
+ }
504
+ let nanos = nanos as u128 ;
495
505
Duration {
496
506
secs : ( nanos / ( NANOS_PER_SEC as u128 ) ) as u64 ,
497
507
nanos : ( nanos % ( NANOS_PER_SEC as u128 ) ) as u32 ,
@@ -512,17 +522,7 @@ impl Duration {
512
522
#[ unstable( feature = "duration_float" , issue = "0" ) ]
513
523
#[ inline]
514
524
pub fn mul_f64 ( self , rhs : f64 ) -> Duration {
515
- let secs = rhs * self . as_float_secs ( ) ;
516
- if !secs. is_finite ( ) {
517
- panic ! ( "got non-finite value when multiplying duration by float" ) ;
518
- }
519
- if secs > MAX_NANOS_F64 {
520
- panic ! ( "overflow when multiplying duration by float" ) ;
521
- }
522
- if secs < 0.0 {
523
- panic ! ( "underflow when multiplying duration by float" ) ;
524
- }
525
- Duration :: from_float_secs ( secs)
525
+ Duration :: from_float_secs ( rhs * self . as_float_secs ( ) )
526
526
}
527
527
528
528
/// Divide `Duration` by `f64`.
@@ -540,17 +540,7 @@ impl Duration {
540
540
#[ unstable( feature = "duration_float" , issue = "0" ) ]
541
541
#[ inline]
542
542
pub fn div_f64 ( self , rhs : f64 ) -> Duration {
543
- let secs = self . as_float_secs ( ) / rhs;
544
- if !secs. is_finite ( ) {
545
- panic ! ( "got non-finite value when dividing duration by float" ) ;
546
- }
547
- if secs > MAX_NANOS_F64 {
548
- panic ! ( "overflow when dividing duration by float" ) ;
549
- }
550
- if secs < 0.0 {
551
- panic ! ( "underflow when multiplying duration by float" ) ;
552
- }
553
- Duration :: from_float_secs ( secs)
543
+ Duration :: from_float_secs ( self . as_float_secs ( ) / rhs)
554
544
}
555
545
556
546
/// Divide `Duration` by `Duration` and return `f64`.
@@ -567,7 +557,7 @@ impl Duration {
567
557
#[ unstable( feature = "duration_float" , issue = "0" ) ]
568
558
#[ inline]
569
559
pub fn div_duration ( self , rhs : Duration ) -> f64 {
570
- self . as_float_secs ( ) / rhs. as_float_secs ( )
560
+ self . as_float_secs ( ) / rhs. as_float_secs ( )
571
561
}
572
562
}
573
563
0 commit comments