Skip to content

Commit 12d8f27

Browse files
committed
review update
1 parent c24fb12 commit 12d8f27

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

src/libcore/time.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
2222
//! ```
2323
24-
use fmt;
24+
use {fmt, u64};
2525
use iter::Sum;
2626
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
27-
use {u64, u128};
2827

2928
const NANOS_PER_SEC: u32 = 1_000_000_000;
3029
const NANOS_PER_MILLI: u32 = 1_000_000;
@@ -517,22 +516,20 @@ impl Mul<f64> for Duration {
517516

518517
fn mul(self, rhs: f64) -> Duration {
519518
const NPS: f64 = NANOS_PER_SEC as f64;
519+
if rhs.is_sign_negative() {
520+
panic!("duration can not be multiplied by negative float");
521+
}
520522
let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64));
521523
if !nanos_f64.is_finite() {
522524
panic!("got non-finite value when multiplying duration by float");
523525
}
524-
if nanos_f64 > (u128::MAX as f64) {
526+
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
525527
panic!("overflow when multiplying duration by float");
526528
};
527529
let nanos_u128 = nanos_f64 as u128;
528-
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
529-
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
530-
if secs > (u64::MAX as u128) {
531-
panic!("overflow when multiplying duration by float");
532-
}
533530
Duration {
534-
secs: secs as u64,
535-
nanos: nanos as u32,
531+
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
532+
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
536533
}
537534
}
538535
}
@@ -543,22 +540,20 @@ impl Mul<Duration> for f64 {
543540

544541
fn mul(self, rhs: Duration) -> Duration {
545542
const NPS: f64 = NANOS_PER_SEC as f64;
543+
if self.is_sign_negative() {
544+
panic!("duration can not be multiplied by negative float");
545+
}
546546
let nanos_f64 = self * (NPS * (rhs.secs as f64) + (rhs.nanos as f64));
547547
if !nanos_f64.is_finite() {
548548
panic!("got non-finite value when multiplying float by duration");
549549
}
550-
if nanos_f64 > (u128::MAX as f64) {
550+
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
551551
panic!("overflow when multiplying float by duration");
552552
};
553553
let nanos_u128 = nanos_f64 as u128;
554-
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
555-
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
556-
if secs > (u64::MAX as u128) {
557-
panic!("overflow when multiplying float by duration");
558-
}
559554
Duration {
560-
secs: secs as u64,
561-
nanos: nanos as u32,
555+
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
556+
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
562557
}
563558
}
564559
}
@@ -592,22 +587,20 @@ impl Div<f64> for Duration {
592587

593588
fn div(self, rhs: f64) -> Duration {
594589
const NPS: f64 = NANOS_PER_SEC as f64;
590+
if rhs.is_sign_negative() {
591+
panic!("duration can not be divided by negative float");
592+
}
595593
let nanos_f64 = (NPS * (self.secs as f64) + (self.nanos as f64)) / rhs;
596594
if !nanos_f64.is_finite() {
597595
panic!("got non-finite value when dividing duration by float");
598596
}
599-
if nanos_f64 > (u128::MAX as f64) {
597+
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
600598
panic!("overflow when dividing duration by float");
601599
};
602600
let nanos_u128 = nanos_f64 as u128;
603-
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
604-
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
605-
if secs > (u64::MAX as u128) {
606-
panic!("overflow when dividing duration by float");
607-
}
608601
Duration {
609-
secs: secs as u64,
610-
nanos: nanos as u32,
602+
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
603+
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
611604
}
612605
}
613606
}

0 commit comments

Comments
 (0)