Skip to content

Commit 9e78cb2

Browse files
authored
move checks to from_float_secs
1 parent 8a0aa9f commit 9e78cb2

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/libcore/time.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
3030
const NANOS_PER_MICRO: u32 = 1_000;
3131
const MILLIS_PER_SEC: u64 = 1_000;
3232
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;
3434

3535
/// A `Duration` type to represent a span of time, typically used for system
3636
/// timeouts.
@@ -491,7 +491,17 @@ impl Duration {
491491
#[unstable(feature = "duration_float", issue = "0")]
492492
#[inline]
493493
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;
495505
Duration {
496506
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
497507
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
@@ -512,17 +522,7 @@ impl Duration {
512522
#[unstable(feature = "duration_float", issue = "0")]
513523
#[inline]
514524
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())
526526
}
527527

528528
/// Divide `Duration` by `f64`.
@@ -540,17 +540,7 @@ impl Duration {
540540
#[unstable(feature = "duration_float", issue = "0")]
541541
#[inline]
542542
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)
554544
}
555545

556546
/// Divide `Duration` by `Duration` and return `f64`.
@@ -567,7 +557,7 @@ impl Duration {
567557
#[unstable(feature = "duration_float", issue = "0")]
568558
#[inline]
569559
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()
571561
}
572562
}
573563

0 commit comments

Comments
 (0)