From 1f1653c3283deca450ee84a01562ef9a914e4478 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: Fri, 3 May 2024 17:31:55 +0300 Subject: [PATCH 1/3] Stabilize `div_duration` --- library/core/src/time.rs | 6 ++---- library/core/tests/lib.rs | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 78494b866b1..e2cf0cb6215 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1062,14 +1062,13 @@ pub fn div_f32(self, rhs: f32) -> Duration { /// /// # Examples /// ``` - /// #![feature(div_duration)] /// 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_f64(dur2), 0.5); /// ``` - #[unstable(feature = "div_duration", issue = "63139")] + #[stable(feature = "div_duration", since = "1.80.0")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] @@ -1082,14 +1081,13 @@ pub const fn div_duration_f64(self, rhs: Duration) -> f64 { /// /// # Examples /// ``` - /// #![feature(div_duration)] /// 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_f32(dur2), 0.5); /// ``` - #[unstable(feature = "div_duration", issue = "63139")] + #[stable(feature = "div_duration", since = "1.80.0")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 797108a8425..b7fcf6bbf4b 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -29,7 +29,6 @@ #![feature(core_private_bignum)] #![feature(core_private_diy_float)] #![feature(dec2flt)] -#![feature(div_duration)] #![feature(duration_abs_diff)] #![feature(duration_consts_float)] #![feature(duration_constants)] From 6d223bb6a1f54eacd57e14d5fc323f97f12e5005 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: Fri, 3 May 2024 17:42:34 +0300 Subject: [PATCH 2/3] Use `CURRENT_RUSTC_VERSION` --- library/core/src/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/time.rs b/library/core/src/time.rs index e2cf0cb6215..a4de3d099b3 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1068,7 +1068,7 @@ pub fn div_f32(self, rhs: f32) -> Duration { /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f64(dur2), 0.5); /// ``` - #[stable(feature = "div_duration", since = "1.80.0")] + #[stable(feature = "div_duration", since = "CURRENT_RUSTC_VERSION")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] @@ -1087,7 +1087,7 @@ pub const fn div_duration_f64(self, rhs: Duration) -> f64 { /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f32(dur2), 0.5); /// ``` - #[stable(feature = "div_duration", since = "1.80.0")] + #[stable(feature = "div_duration", since = "CURRENT_RUSTC_VERSION")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] From 8da41b107debc95250078049eae63e5ab3601476 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 15 May 2024 00:38:34 +0300 Subject: [PATCH 3/3] Divide float nanoseconds instead of seconds --- library/core/src/time.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/core/src/time.rs b/library/core/src/time.rs index a4de3d099b3..bbeacac0510 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1074,7 +1074,9 @@ pub fn div_f32(self, rhs: f32) -> Duration { #[inline] #[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")] pub const fn div_duration_f64(self, rhs: Duration) -> f64 { - self.as_secs_f64() / rhs.as_secs_f64() + let self_nanos = (self.secs as f64) * (NANOS_PER_SEC as f64) + (self.nanos.0 as f64); + let rhs_nanos = (rhs.secs as f64) * (NANOS_PER_SEC as f64) + (rhs.nanos.0 as f64); + self_nanos / rhs_nanos } /// Divide `Duration` by `Duration` and return `f32`. @@ -1093,7 +1095,9 @@ pub const fn div_duration_f64(self, rhs: Duration) -> f64 { #[inline] #[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")] pub const fn div_duration_f32(self, rhs: Duration) -> f32 { - self.as_secs_f32() / rhs.as_secs_f32() + let self_nanos = (self.secs as f32) * (NANOS_PER_SEC as f32) + (self.nanos.0 as f32); + let rhs_nanos = (rhs.secs as f32) * (NANOS_PER_SEC as f32) + (rhs.nanos.0 as f32); + self_nanos / rhs_nanos } }