From c24fb126e7cdd73163af67c264bf626aebbeee84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sun, 29 Jul 2018 07:00:13 +0300 Subject: [PATCH 01/18] duration div mul extras --- src/libcore/time.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++ src/libstd/time.rs | 26 ++++++++++ 2 files changed, 141 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 54973b7b7783a..fcd2726b84dea 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -24,6 +24,7 @@ use fmt; 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; @@ -501,6 +502,67 @@ impl Mul for Duration { } } +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl Mul for u32 { + type Output = Duration; + + fn mul(self, rhs: Duration) -> Duration { + rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") + } +} + +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl Mul for Duration { + type Output = Duration; + + fn mul(self, rhs: f64) -> Duration { + const NPS: f64 = NANOS_PER_SEC as f64; + 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) { + 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, + } + } +} + +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl Mul for f64 { + type Output = Duration; + + fn mul(self, rhs: Duration) -> Duration { + const NPS: f64 = NANOS_PER_SEC as f64; + 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) { + 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, + } + } +} + #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl MulAssign for Duration { fn mul_assign(&mut self, rhs: u32) { @@ -508,6 +570,13 @@ impl MulAssign for Duration { } } +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl MulAssign for Duration { + fn mul_assign(&mut self, rhs: f64) { + *self = *self * rhs; + } +} + #[stable(feature = "duration", since = "1.3.0")] impl Div for Duration { type Output = Duration; @@ -517,6 +586,44 @@ impl Div for Duration { } } +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl Div for Duration { + type Output = Duration; + + fn div(self, rhs: f64) -> Duration { + const NPS: f64 = NANOS_PER_SEC as f64; + 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) { + 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, + } + } +} + +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl Div for Duration { + type Output = f64; + + fn div(self, rhs: Duration) -> f64 { + const NPS: f64 = NANOS_PER_SEC as f64; + let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64); + let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64); + nanos1/nanos2 + } +} + #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl DivAssign for Duration { fn div_assign(&mut self, rhs: u32) { @@ -524,6 +631,14 @@ impl DivAssign for Duration { } } +#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +impl DivAssign for Duration { + fn div_assign(&mut self, rhs: f64) { + *self = *self / rhs; + } +} + + macro_rules! sum_durations { ($iter:expr) => {{ let mut total_secs: u64 = 0; diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 90ab349159915..640902426cdd2 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -590,4 +590,30 @@ mod tests { let hundred_twenty_years = thirty_years * 4; assert!(a < hundred_twenty_years); } + + #[test] + fn duration_float_ops() { + let dur = Duration::new(2, 700_000_000); + + let dur2 = 3.14*dur; + assert_eq!(dur2, dur*3.14); + assert_eq!(dur2.as_secs(), 8); + assert_eq!(dur2.subsec_nanos(), 478_000_000); + + let dur3 = 3.14e5*dur; + assert_eq!(dur3, dur*3.14e5); + assert_eq!(dur3.as_secs(), 847_800); + assert_eq!(dur3.subsec_nanos(), 0); + + let dur4 = dur/3.14; + assert_eq!(dur4.as_secs(), 0); + assert_eq!(dur4.subsec_nanos(), 859_872_611); + + let dur5 = dur/3.14e5; + assert_eq!(dur5.as_secs(), 0); + // we are using truncation and not rounding + assert_eq!(dur5.subsec_nanos(), 8598); + + assert_eq!(dur/Duration::new(5, 400_000_000), 0.5); + } } From 12d8f2792a677e3e3beabec780cb2d93719095f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sun, 29 Jul 2018 07:35:52 +0300 Subject: [PATCH 02/18] review update --- src/libcore/time.rs | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index fcd2726b84dea..56e45146f5911 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -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; @@ -517,22 +516,20 @@ impl Mul 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, } } } @@ -543,22 +540,20 @@ impl Mul 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, } } } @@ -592,22 +587,20 @@ impl Div 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, } } } From 3e07236a31950aa8e6a4c66066de02d1bca68ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sun, 29 Jul 2018 16:01:43 +0300 Subject: [PATCH 03/18] add MAX_NANOS_F64 constant --- src/libcore/time.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 56e45146f5911..4777b4356376b 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -30,6 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000; const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; +const MAX_NANOS_F64: f64 = ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -523,7 +524,7 @@ impl Mul for Duration { if !nanos_f64.is_finite() { panic!("got non-finite value when multiplying duration by float"); } - if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 { + if nanos_f64 > MAX_NANOS_F64 { panic!("overflow when multiplying duration by float"); }; let nanos_u128 = nanos_f64 as u128; @@ -547,7 +548,7 @@ impl Mul for f64 { if !nanos_f64.is_finite() { panic!("got non-finite value when multiplying float by duration"); } - if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 { + if nanos_f64 > MAX_NANOS_F64 { panic!("overflow when multiplying float by duration"); }; let nanos_u128 = nanos_f64 as u128; @@ -594,7 +595,7 @@ impl Div for Duration { if !nanos_f64.is_finite() { panic!("got non-finite value when dividing duration by float"); } - if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 { + if nanos_f64 > MAX_NANOS_F64 { panic!("overflow when dividing duration by float"); }; let nanos_u128 = nanos_f64 as u128; From 2c300fa15b65e75ca07ada06819502ced7d80882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 30 Jul 2018 12:12:52 +0300 Subject: [PATCH 04/18] change negativity check --- src/libcore/time.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 4777b4356376b..cd98f51f5cdb7 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -517,16 +517,16 @@ impl Mul 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 > MAX_NANOS_F64 { panic!("overflow when multiplying duration by float"); - }; + } + if nanos_f64 < 0.0 { + panic!("underflow when multiplying duration by float"); + } let nanos_u128 = nanos_f64 as u128; Duration { secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, @@ -541,16 +541,16 @@ impl Mul 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 > MAX_NANOS_F64 { panic!("overflow when multiplying float by duration"); - }; + } + if nanos_f64 < 0.0 { + panic!("underflow when multiplying float by duration"); + } let nanos_u128 = nanos_f64 as u128; Duration { secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, @@ -588,16 +588,16 @@ impl Div 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 > MAX_NANOS_F64 { panic!("overflow when dividing duration by float"); - }; + } + if nanos_f64 < 0.0 { + panic!("underflow when multiplying duration by float"); + } let nanos_u128 = nanos_f64 as u128; Duration { secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, From 2cab0deb97a09ace497c16303095f9905e1d6807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Tue, 31 Jul 2018 04:07:11 +0300 Subject: [PATCH 05/18] don't duplicate impls --- src/libcore/time.rs | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index cd98f51f5cdb7..fff47ac44ef69 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -507,7 +507,7 @@ impl Mul for u32 { type Output = Duration; fn mul(self, rhs: Duration) -> Duration { - rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") + rhs * self } } @@ -540,22 +540,7 @@ impl Mul for f64 { type Output = Duration; fn mul(self, rhs: Duration) -> Duration { - const NPS: f64 = NANOS_PER_SEC as f64; - 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 > MAX_NANOS_F64 { - panic!("overflow when multiplying float by duration"); - } - if nanos_f64 < 0.0 { - panic!("underflow when multiplying float by duration"); - } - let nanos_u128 = nanos_f64 as u128; - Duration { - secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, - nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, - } + rhs * self } } From d48a649a17401d5c5fc25e9484b41f8b4643e6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Sat, 4 Aug 2018 00:47:13 +0300 Subject: [PATCH 06/18] 1.29.0 -> 1.30.0 --- src/libcore/time.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index fff47ac44ef69..6ab8e18adc8ed 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -502,7 +502,7 @@ impl Mul for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl Mul for u32 { type Output = Duration; @@ -511,7 +511,7 @@ impl Mul for u32 { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl Mul for Duration { type Output = Duration; @@ -535,7 +535,7 @@ impl Mul for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl Mul for f64 { type Output = Duration; @@ -551,7 +551,7 @@ impl MulAssign for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl MulAssign for Duration { fn mul_assign(&mut self, rhs: f64) { *self = *self * rhs; @@ -567,7 +567,7 @@ impl Div for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl Div for Duration { type Output = Duration; @@ -591,7 +591,7 @@ impl Div for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl Div for Duration { type Output = f64; @@ -610,7 +610,7 @@ impl DivAssign for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.29.0")] +#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] impl DivAssign for Duration { fn div_assign(&mut self, rhs: f64) { *self = *self / rhs; From 0673417daa13cc926e2cf618627675e76730e3f8 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 11:50:46 +0300 Subject: [PATCH 07/18] Move float ops to unstable inherent methods --- src/libcore/time.rs | 167 ++++++++++++++++++++++---------------------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 6ab8e18adc8ed..64cb13c7eae75 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -459,6 +459,88 @@ impl Duration { None } } + + /// Multiply `Duration` by `f64`. + /// + /// # Examples + /// ``` + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); + /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); + /// ``` + #[unstable(feature = "duration_float_ops", + reason = "duration/floats operations are unstabe", + issue = "0")] + #[inline] + pub fn mul_f64(self, rhs: f64) -> Duration { + const NPS: f64 = NANOS_PER_SEC as f64; + 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 > MAX_NANOS_F64 { + panic!("overflow when multiplying duration by float"); + } + if nanos_f64 < 0.0 { + panic!("underflow when multiplying duration by float"); + } + let nanos_u128 = nanos_f64 as u128; + Duration { + secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, + nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, + } + } + + /// Divide `Duration` by `f64`. + /// + /// # Examples + /// ``` + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611)); + /// // note that truncation is used, not rounding + /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598)); + /// ``` + #[unstable(feature = "duration_float_ops", + reason = "duration/floats operations are unstabe", + issue = "0")] + #[inline] + pub fn div_f64(self, rhs: f64) -> Duration { + const NPS: f64 = NANOS_PER_SEC as f64; + 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 > MAX_NANOS_F64 { + panic!("overflow when dividing duration by float"); + } + if nanos_f64 < 0.0 { + panic!("underflow when multiplying duration by float"); + } + let nanos_u128 = nanos_f64 as u128; + Duration { + secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, + nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, + } + } + + /// Divide `Duration` by `Duration` and return `f64`. + /// + /// # Examples + /// ``` + /// let dur1 = Duration::new(2, 700_000_000); + /// let dur2 = Duration::new(5, 400_000_000); + /// assert_eq!(dur1.div_duration(dur2), 0.5); + /// ``` + #[unstable(feature = "duration_float_ops", + reason = "duration/floats operations are unstabe", + issue = "0")] + #[inline] + pub fn div_duration(self, rhs: Duration) -> f64 { + const NPS: f64 = NANOS_PER_SEC as f64; + let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64); + let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64); + nanos1/nanos2 + } } #[stable(feature = "duration", since = "1.3.0")] @@ -502,7 +584,7 @@ impl Mul for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] +#[stable(feature = "symmetric_u32_duration_mul", since = "1.30.0")] impl Mul for u32 { type Output = Duration; @@ -511,39 +593,6 @@ impl Mul for u32 { } } -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl Mul for Duration { - type Output = Duration; - - fn mul(self, rhs: f64) -> Duration { - const NPS: f64 = NANOS_PER_SEC as f64; - 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 > MAX_NANOS_F64 { - panic!("overflow when multiplying duration by float"); - } - if nanos_f64 < 0.0 { - panic!("underflow when multiplying duration by float"); - } - let nanos_u128 = nanos_f64 as u128; - Duration { - secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, - nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, - } - } -} - -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl Mul for f64 { - type Output = Duration; - - fn mul(self, rhs: Duration) -> Duration { - rhs * self - } -} - #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl MulAssign for Duration { fn mul_assign(&mut self, rhs: u32) { @@ -551,13 +600,6 @@ impl MulAssign for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl MulAssign for Duration { - fn mul_assign(&mut self, rhs: f64) { - *self = *self * rhs; - } -} - #[stable(feature = "duration", since = "1.3.0")] impl Div for Duration { type Output = Duration; @@ -567,42 +609,6 @@ impl Div for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl Div for Duration { - type Output = Duration; - - fn div(self, rhs: f64) -> Duration { - const NPS: f64 = NANOS_PER_SEC as f64; - 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 > MAX_NANOS_F64 { - panic!("overflow when dividing duration by float"); - } - if nanos_f64 < 0.0 { - panic!("underflow when multiplying duration by float"); - } - let nanos_u128 = nanos_f64 as u128; - Duration { - secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, - nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, - } - } -} - -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl Div for Duration { - type Output = f64; - - fn div(self, rhs: Duration) -> f64 { - const NPS: f64 = NANOS_PER_SEC as f64; - let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64); - let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64); - nanos1/nanos2 - } -} - #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl DivAssign for Duration { fn div_assign(&mut self, rhs: u32) { @@ -610,13 +616,6 @@ impl DivAssign for Duration { } } -#[stable(feature = "duration_mul_div_extras", since = "1.30.0")] -impl DivAssign for Duration { - fn div_assign(&mut self, rhs: f64) { - *self = *self / rhs; - } -} - macro_rules! sum_durations { ($iter:expr) => {{ From 36dff2a5dec66d783ca575495c7af051f2d7de0c Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 11:51:33 +0300 Subject: [PATCH 08/18] Remove tests --- src/libstd/time.rs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 640902426cdd2..90ab349159915 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -590,30 +590,4 @@ mod tests { let hundred_twenty_years = thirty_years * 4; assert!(a < hundred_twenty_years); } - - #[test] - fn duration_float_ops() { - let dur = Duration::new(2, 700_000_000); - - let dur2 = 3.14*dur; - assert_eq!(dur2, dur*3.14); - assert_eq!(dur2.as_secs(), 8); - assert_eq!(dur2.subsec_nanos(), 478_000_000); - - let dur3 = 3.14e5*dur; - assert_eq!(dur3, dur*3.14e5); - assert_eq!(dur3.as_secs(), 847_800); - assert_eq!(dur3.subsec_nanos(), 0); - - let dur4 = dur/3.14; - assert_eq!(dur4.as_secs(), 0); - assert_eq!(dur4.subsec_nanos(), 859_872_611); - - let dur5 = dur/3.14e5; - assert_eq!(dur5.as_secs(), 0); - // we are using truncation and not rounding - assert_eq!(dur5.subsec_nanos(), 8598); - - assert_eq!(dur/Duration::new(5, 400_000_000), 0.5); - } } From 206ca68ff38604325b4479f8b66cbd50cf4c693b Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 11:52:19 +0300 Subject: [PATCH 09/18] remove newline --- src/libcore/time.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 64cb13c7eae75..db5d948087814 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -616,7 +616,6 @@ impl DivAssign for Duration { } } - macro_rules! sum_durations { ($iter:expr) => {{ let mut total_secs: u64 = 0; From 07c15ea64552bdf78bcbb5224331c03cec7e37a2 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 11:56:39 +0300 Subject: [PATCH 10/18] more explicit impl --- src/libcore/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index db5d948087814..e85ac33a4196e 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -589,7 +589,7 @@ impl Mul for u32 { type Output = Duration; fn mul(self, rhs: Duration) -> Duration { - rhs * self + rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") } } From c5cbea69aa4426f8b1929e2a907ee48c34013047 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 14:17:36 +0300 Subject: [PATCH 11/18] fix doctests --- src/libcore/time.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index e85ac33a4196e..294c999269740 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -464,6 +464,8 @@ impl Duration { /// /// # Examples /// ``` + /// use std::time::Duration; + /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); @@ -495,6 +497,8 @@ impl Duration { /// /// # Examples /// ``` + /// use std::time::Duration; + /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611)); /// // note that truncation is used, not rounding @@ -527,6 +531,8 @@ impl Duration { /// /// # Examples /// ``` + /// use std::time::Duration; + /// /// let dur1 = Duration::new(2, 700_000_000); /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration(dur2), 0.5); From 533c0f0d1f0501dbd0ca76fd4231660835d9e83b Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 17:10:38 +0300 Subject: [PATCH 12/18] fix tests --- src/libcore/time.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 294c999269740..71eb00781f03b 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -464,6 +464,7 @@ impl Duration { /// /// # Examples /// ``` + /// #![feature(exact_chunks)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -497,6 +498,7 @@ impl Duration { /// /// # Examples /// ``` + /// #![feature(exact_chunks)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -531,6 +533,7 @@ impl Duration { /// /// # Examples /// ``` + /// #![feature(exact_chunks)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); @@ -595,7 +598,7 @@ impl Mul for u32 { type Output = Duration; fn mul(self, rhs: Duration) -> Duration { - rhs.checked_mul(self).expect("overflow when multiplying scalar by duration") + rhs * self } } From c11281f1888428551ddf9c60d0030deb63be4b78 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 12 Sep 2018 18:33:48 +0300 Subject: [PATCH 13/18] fix tests --- src/libcore/time.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 71eb00781f03b..cddc12ff58177 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -464,7 +464,7 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(exact_chunks)] + /// #![feature(duration_float_ops)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -498,7 +498,7 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(exact_chunks)] + /// #![feature(duration_float_ops)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -533,7 +533,7 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(exact_chunks)] + /// #![feature(duration_float_ops)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); From 37972ae3006a1cb67ff94970c2d034063b3eb895 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Thu, 13 Sep 2018 00:43:53 +0000 Subject: [PATCH 14/18] add as_float_secs and from_float_secs methods, refactor float methods --- src/libcore/time.rs | 91 +++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index cddc12ff58177..94a167f08f700 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -459,46 +459,77 @@ impl Duration { None } } + + /// Returns the number of seconds contained by this `Duration` as `f64`. + /// + /// The returned value does include the fractional (nanosecond) part of the duration. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.as_float_secs(), 2.7); + /// ``` + #[unstable(feature = "duration_float", issue = "0")] + #[inline] + pub fn as_float_secs(&self) -> f64 { + (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64) + } + /// Creates a new `Duration` from the specified number of seconds. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::from_float_secs(2.7); + /// assert_eq!(dur, Duration::new(2, 700_000_000)); + /// ``` + #[unstable(feature = "duration_float", issue = "0")] + #[inline] + pub fn from_float_secs(secs: f64) -> Duration { + let nanos = (secs * (NANOS_PER_SEC as f64)) as u128; + Duration { + secs: (nanos / (NANOS_PER_SEC as u128)) as u64, + nanos: (nanos % (NANOS_PER_SEC as u128)) as u32, + } + } + /// Multiply `Duration` by `f64`. /// /// # Examples /// ``` - /// #![feature(duration_float_ops)] + /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); /// ``` - #[unstable(feature = "duration_float_ops", - reason = "duration/floats operations are unstabe", - issue = "0")] + #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn mul_f64(self, rhs: f64) -> Duration { - const NPS: f64 = NANOS_PER_SEC as f64; - let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64)); - if !nanos_f64.is_finite() { + let secs = rhs * self.as_float_secs(); + if !secs.is_finite() { panic!("got non-finite value when multiplying duration by float"); } - if nanos_f64 > MAX_NANOS_F64 { + if secs > MAX_NANOS_F64 { panic!("overflow when multiplying duration by float"); } - if nanos_f64 < 0.0 { + if secs < 0.0 { panic!("underflow when multiplying duration by float"); } - let nanos_u128 = nanos_f64 as u128; - Duration { - secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, - nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, - } + Duration::from_float_secs(secs) } /// Divide `Duration` by `f64`. /// /// # Examples /// ``` - /// #![feature(duration_float_ops)] + /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -506,49 +537,37 @@ impl Duration { /// // note that truncation is used, not rounding /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598)); /// ``` - #[unstable(feature = "duration_float_ops", - reason = "duration/floats operations are unstabe", - issue = "0")] + #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn div_f64(self, rhs: f64) -> Duration { - const NPS: f64 = NANOS_PER_SEC as f64; - let nanos_f64 = (NPS * (self.secs as f64) + (self.nanos as f64)) / rhs; - if !nanos_f64.is_finite() { + let secs = self.as_float_secs() / rhs; + if !secs.is_finite() { panic!("got non-finite value when dividing duration by float"); } - if nanos_f64 > MAX_NANOS_F64 { + if secs > MAX_NANOS_F64 { panic!("overflow when dividing duration by float"); } - if nanos_f64 < 0.0 { + if secs < 0.0 { panic!("underflow when multiplying duration by float"); } - let nanos_u128 = nanos_f64 as u128; - Duration { - secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64, - nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32, - } + Duration::from_float_secs(secs) } /// Divide `Duration` by `Duration` and return `f64`. /// /// # Examples /// ``` - /// #![feature(duration_float_ops)] + /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration(dur2), 0.5); /// ``` - #[unstable(feature = "duration_float_ops", - reason = "duration/floats operations are unstabe", - issue = "0")] + #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn div_duration(self, rhs: Duration) -> f64 { - const NPS: f64 = NANOS_PER_SEC as f64; - let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64); - let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64); - nanos1/nanos2 + self.as_float_secs()/rhs.as_float_secs() } } From 8a0aa9f3ae2459f15270638add69bdc7619cd327 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Thu, 13 Sep 2018 00:52:59 +0000 Subject: [PATCH 15/18] remove trailing spaces --- src/libcore/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 94a167f08f700..b9b45e39e4023 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -459,7 +459,7 @@ impl Duration { None } } - + /// Returns the number of seconds contained by this `Duration` as `f64`. /// /// The returned value does include the fractional (nanosecond) part of the duration. @@ -497,7 +497,7 @@ impl Duration { nanos: (nanos % (NANOS_PER_SEC as u128)) as u32, } } - + /// Multiply `Duration` by `f64`. /// /// # Examples From 9e78cb24461177ee1477abcbda01e3c4158722d1 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Thu, 13 Sep 2018 01:40:38 +0000 Subject: [PATCH 16/18] move checks to from_float_secs --- src/libcore/time.rs | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index b9b45e39e4023..f0e4b29700b46 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -30,7 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000; const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; -const MAX_NANOS_F64: f64 = ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64; +const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128) - 1) as f64; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -491,7 +491,17 @@ impl Duration { #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn from_float_secs(secs: f64) -> Duration { - let nanos = (secs * (NANOS_PER_SEC as f64)) as u128; + let nanos = secs * (NANOS_PER_SEC as f64); + if !nanos.is_finite() { + panic!("got non-finite value when converting float to duration"); + } + if nanos > MAX_NANOS_F64 { + panic!("overflow when converting float to duration"); + } + if nanos < 0.0 { + panic!("underflow when converting float to duration"); + } + let nanos = nanos as u128; Duration { secs: (nanos / (NANOS_PER_SEC as u128)) as u64, nanos: (nanos % (NANOS_PER_SEC as u128)) as u32, @@ -512,17 +522,7 @@ impl Duration { #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn mul_f64(self, rhs: f64) -> Duration { - let secs = rhs * self.as_float_secs(); - if !secs.is_finite() { - panic!("got non-finite value when multiplying duration by float"); - } - if secs > MAX_NANOS_F64 { - panic!("overflow when multiplying duration by float"); - } - if secs < 0.0 { - panic!("underflow when multiplying duration by float"); - } - Duration::from_float_secs(secs) + Duration::from_float_secs(rhs * self.as_float_secs()) } /// Divide `Duration` by `f64`. @@ -540,17 +540,7 @@ impl Duration { #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn div_f64(self, rhs: f64) -> Duration { - let secs = self.as_float_secs() / rhs; - if !secs.is_finite() { - panic!("got non-finite value when dividing duration by float"); - } - if secs > MAX_NANOS_F64 { - panic!("overflow when dividing duration by float"); - } - if secs < 0.0 { - panic!("underflow when multiplying duration by float"); - } - Duration::from_float_secs(secs) + Duration::from_float_secs(self.as_float_secs() / rhs) } /// Divide `Duration` by `Duration` and return `f64`. @@ -567,7 +557,7 @@ impl Duration { #[unstable(feature = "duration_float", issue = "0")] #[inline] pub fn div_duration(self, rhs: Duration) -> f64 { - self.as_float_secs()/rhs.as_float_secs() + self.as_float_secs() / rhs.as_float_secs() } } From 2aca69757f078ac3aad9c2aff3c357ca35e5c486 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Thu, 13 Sep 2018 01:47:08 +0000 Subject: [PATCH 17/18] add panics section to method docs --- src/libcore/time.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index f0e4b29700b46..e2990c8660e75 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -480,6 +480,9 @@ impl Duration { /// Creates a new `Duration` from the specified number of seconds. /// + /// # Panics + /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`. + /// /// # Examples /// ``` /// #![feature(duration_float)] @@ -510,6 +513,9 @@ impl Duration { /// Multiply `Duration` by `f64`. /// + /// # Panics + /// This method will panic if result is not finite, negative or overflows `Duration`. + /// /// # Examples /// ``` /// #![feature(duration_float)] @@ -527,6 +533,9 @@ impl Duration { /// Divide `Duration` by `f64`. /// + /// # Panics + /// This method will panic if result is not finite, negative or overflows `Duration`. + /// /// # Examples /// ``` /// #![feature(duration_float)] From fd7565b076440829b86cc7bc5f2457bf42d43936 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 19 Sep 2018 18:40:33 +0300 Subject: [PATCH 18/18] Added tracking issue, fixed check, 1.30 -> 1.31 --- src/libcore/time.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index e2990c8660e75..12c29a324b87e 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -30,7 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000; const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; -const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128) - 1) as f64; +const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -472,7 +472,7 @@ impl Duration { /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.as_float_secs(), 2.7); /// ``` - #[unstable(feature = "duration_float", issue = "0")] + #[unstable(feature = "duration_float", issue = "54361")] #[inline] pub fn as_float_secs(&self) -> f64 { (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64) @@ -491,14 +491,14 @@ impl Duration { /// let dur = Duration::from_float_secs(2.7); /// assert_eq!(dur, Duration::new(2, 700_000_000)); /// ``` - #[unstable(feature = "duration_float", issue = "0")] + #[unstable(feature = "duration_float", issue = "54361")] #[inline] pub fn from_float_secs(secs: f64) -> Duration { let nanos = secs * (NANOS_PER_SEC as f64); if !nanos.is_finite() { panic!("got non-finite value when converting float to duration"); } - if nanos > MAX_NANOS_F64 { + if nanos >= MAX_NANOS_F64 { panic!("overflow when converting float to duration"); } if nanos < 0.0 { @@ -525,7 +525,7 @@ impl Duration { /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); /// ``` - #[unstable(feature = "duration_float", issue = "0")] + #[unstable(feature = "duration_float", issue = "54361")] #[inline] pub fn mul_f64(self, rhs: f64) -> Duration { Duration::from_float_secs(rhs * self.as_float_secs()) @@ -546,7 +546,7 @@ impl Duration { /// // note that truncation is used, not rounding /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598)); /// ``` - #[unstable(feature = "duration_float", issue = "0")] + #[unstable(feature = "duration_float", issue = "54361")] #[inline] pub fn div_f64(self, rhs: f64) -> Duration { Duration::from_float_secs(self.as_float_secs() / rhs) @@ -563,7 +563,7 @@ impl Duration { /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration(dur2), 0.5); /// ``` - #[unstable(feature = "duration_float", issue = "0")] + #[unstable(feature = "duration_float", issue = "54361")] #[inline] pub fn div_duration(self, rhs: Duration) -> f64 { self.as_float_secs() / rhs.as_float_secs() @@ -611,7 +611,7 @@ impl Mul for Duration { } } -#[stable(feature = "symmetric_u32_duration_mul", since = "1.30.0")] +#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")] impl Mul for u32 { type Output = Duration;