Skip to content

Commit

Permalink
review update
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jul 29, 2018
1 parent c24fb12 commit 12d8f27
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
//! ```

use fmt;
use {fmt, u64};
use iter::Sum;
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
use {u64, u128};

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

fn mul(self, rhs: f64) -> Duration {
const NPS: f64 = NANOS_PER_SEC as f64;
if rhs.is_sign_negative() {
panic!("duration can not be multiplied by negative float");
}
let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64));
if !nanos_f64.is_finite() {
panic!("got non-finite value when multiplying duration by float");
}
if nanos_f64 > (u128::MAX as f64) {
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when multiplying duration by float");
};
let nanos_u128 = nanos_f64 as u128;
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
if secs > (u64::MAX as u128) {
panic!("overflow when multiplying duration by float");
}
Duration {
secs: secs as u64,
nanos: nanos as u32,
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}
Expand All @@ -543,22 +540,20 @@ impl Mul<Duration> for f64 {

fn mul(self, rhs: Duration) -> Duration {
const NPS: f64 = NANOS_PER_SEC as f64;
if self.is_sign_negative() {
panic!("duration can not be multiplied by negative float");
}
let nanos_f64 = self * (NPS * (rhs.secs as f64) + (rhs.nanos as f64));
if !nanos_f64.is_finite() {
panic!("got non-finite value when multiplying float by duration");
}
if nanos_f64 > (u128::MAX as f64) {
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when multiplying float by duration");
};
let nanos_u128 = nanos_f64 as u128;
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
if secs > (u64::MAX as u128) {
panic!("overflow when multiplying float by duration");
}
Duration {
secs: secs as u64,
nanos: nanos as u32,
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}
Expand Down Expand Up @@ -592,22 +587,20 @@ impl Div<f64> for Duration {

fn div(self, rhs: f64) -> Duration {
const NPS: f64 = NANOS_PER_SEC as f64;
if rhs.is_sign_negative() {
panic!("duration can not be divided by negative float");
}
let nanos_f64 = (NPS * (self.secs as f64) + (self.nanos as f64)) / rhs;
if !nanos_f64.is_finite() {
panic!("got non-finite value when dividing duration by float");
}
if nanos_f64 > (u128::MAX as f64) {
if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
panic!("overflow when dividing duration by float");
};
let nanos_u128 = nanos_f64 as u128;
let secs = nanos_u128 / (NANOS_PER_SEC as u128);
let nanos = nanos_u128 % (NANOS_PER_SEC as u128);
if secs > (u64::MAX as u128) {
panic!("overflow when dividing duration by float");
}
Duration {
secs: secs as u64,
nanos: nanos as u32,
secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
}
}
}
Expand Down

0 comments on commit 12d8f27

Please sign in to comment.