Skip to content

Commit

Permalink
Partially stabilize duration_consts_2
Browse files Browse the repository at this point in the history
Methods that were only blocked on `const_panic` have been stabilized.
The remaining methods of `duration_consts_2` are all related to floats,
and as such have been placed behind the `duration_consts_float` feature
gate.
  • Loading branch information
jhpratt committed Oct 16, 2021
1 parent 7fbd4ce commit 14bb025
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
#![feature(const_type_id)]
#![feature(const_type_name)]
#![feature(const_default_impls)]
#![feature(duration_consts_2)]
#![feature(duration_consts_float)]
#![feature(ptr_metadata)]
#![feature(slice_ptr_get)]
#![feature(variant_count)]
Expand Down
41 changes: 23 additions & 18 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ impl Duration {
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[must_use]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_panic))]
pub const fn new(secs: u64, nanos: u32) -> Duration {
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
Some(secs) => secs,
Expand Down Expand Up @@ -477,7 +478,8 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_panic))]
pub const fn checked_add(self, rhs: Duration) -> Option<Duration> {
if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
let mut nanos = self.nanos + rhs.nanos;
Expand Down Expand Up @@ -512,7 +514,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
pub const fn saturating_add(self, rhs: Duration) -> Duration {
match self.checked_add(rhs) {
Some(res) => res,
Expand All @@ -537,7 +539,8 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_panic))]
pub const fn checked_sub(self, rhs: Duration) -> Option<Duration> {
if let Some(mut secs) = self.secs.checked_sub(rhs.secs) {
let nanos = if self.nanos >= rhs.nanos {
Expand Down Expand Up @@ -570,7 +573,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
pub const fn saturating_sub(self, rhs: Duration) -> Duration {
match self.checked_sub(rhs) {
Some(res) => res,
Expand All @@ -595,7 +598,8 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_panic))]
pub const fn checked_mul(self, rhs: u32) -> Option<Duration> {
// Multiply nanoseconds as u64, because it cannot overflow that way.
let total_nanos = self.nanos as u64 * rhs as u64;
Expand Down Expand Up @@ -626,7 +630,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
pub const fn saturating_mul(self, rhs: u32) -> Duration {
match self.checked_mul(rhs) {
Some(res) => res,
Expand All @@ -652,7 +656,8 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_panic))]
pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
if rhs != 0 {
let secs = self.secs / (rhs as u64);
Expand Down Expand Up @@ -680,7 +685,7 @@ impl Duration {
#[stable(feature = "duration_float", since = "1.38.0")]
#[must_use]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn as_secs_f64(&self) -> f64 {
(self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
}
Expand All @@ -699,7 +704,7 @@ impl Duration {
#[stable(feature = "duration_float", since = "1.38.0")]
#[must_use]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn as_secs_f32(&self) -> f32 {
(self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32)
}
Expand All @@ -720,7 +725,7 @@ impl Duration {
#[stable(feature = "duration_float", since = "1.38.0")]
#[must_use]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn from_secs_f64(secs: f64) -> Duration {
match Duration::try_from_secs_f64(secs) {
Ok(v) => v,
Expand Down Expand Up @@ -782,7 +787,7 @@ impl Duration {
#[stable(feature = "duration_float", since = "1.38.0")]
#[must_use]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn from_secs_f32(secs: f32) -> Duration {
match Duration::try_from_secs_f32(secs) {
Ok(v) => v,
Expand Down Expand Up @@ -845,7 +850,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn mul_f64(self, rhs: f64) -> Duration {
Duration::from_secs_f64(rhs * self.as_secs_f64())
}
Expand All @@ -869,7 +874,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn mul_f32(self, rhs: f32) -> Duration {
Duration::from_secs_f32(rhs * self.as_secs_f32())
}
Expand All @@ -892,7 +897,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn div_f64(self, rhs: f64) -> Duration {
Duration::from_secs_f64(self.as_secs_f64() / rhs)
}
Expand All @@ -917,7 +922,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
pub const fn div_f32(self, rhs: f32) -> Duration {
Duration::from_secs_f32(self.as_secs_f32() / rhs)
}
Expand All @@ -937,7 +942,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[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()
}
Expand All @@ -957,7 +962,7 @@ impl Duration {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
#[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()
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#![feature(core_private_diy_float)]
#![feature(dec2flt)]
#![feature(div_duration)]
#![feature(duration_consts_2)]
#![feature(duration_consts_float)]
#![feature(duration_constants)]
#![feature(exact_size_is_empty)]
#![feature(extern_types)]
Expand Down

0 comments on commit 14bb025

Please sign in to comment.