Skip to content

Commit bc42cd8

Browse files
authored
Rollup merge of rust-lang#76335 - CDirkx:const-duration, r=ecstatic-morse
Make all methods of `Duration` unstably const Make the following methods of `Duration` unstable const under `duration_const_2`: - `from_secs_f64` - `from_secs_f32` - `mul_f64` - `mul_f32` - `div_f64` - `div_f32` This results in all methods of `Duration` being (unstable) const. Moved the tests to `library` as part of rust-lang#76268. Possible because of rust-lang#72449, which made the relevant `f32` and `f64` methods const. Tracking issue: rust-lang#72440 r? @ecstatic-morse
2 parents f2d9579 + 73e0a56 commit bc42cd8

File tree

4 files changed

+116
-73
lines changed

4 files changed

+116
-73
lines changed

library/core/src/time.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ impl Duration {
693693
/// ```
694694
#[stable(feature = "duration_float", since = "1.38.0")]
695695
#[inline]
696-
pub fn from_secs_f64(secs: f64) -> Duration {
696+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
697+
pub const fn from_secs_f64(secs: f64) -> Duration {
697698
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f64;
698699
let nanos = secs * (NANOS_PER_SEC as f64);
699700
if !nanos.is_finite() {
@@ -727,7 +728,8 @@ impl Duration {
727728
/// ```
728729
#[stable(feature = "duration_float", since = "1.38.0")]
729730
#[inline]
730-
pub fn from_secs_f32(secs: f32) -> Duration {
731+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
732+
pub const fn from_secs_f32(secs: f32) -> Duration {
731733
const MAX_NANOS_F32: f32 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f32;
732734
let nanos = secs * (NANOS_PER_SEC as f32);
733735
if !nanos.is_finite() {
@@ -761,7 +763,8 @@ impl Duration {
761763
/// ```
762764
#[stable(feature = "duration_float", since = "1.38.0")]
763765
#[inline]
764-
pub fn mul_f64(self, rhs: f64) -> Duration {
766+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
767+
pub const fn mul_f64(self, rhs: f64) -> Duration {
765768
Duration::from_secs_f64(rhs * self.as_secs_f64())
766769
}
767770

@@ -782,7 +785,8 @@ impl Duration {
782785
/// ```
783786
#[stable(feature = "duration_float", since = "1.38.0")]
784787
#[inline]
785-
pub fn mul_f32(self, rhs: f32) -> Duration {
788+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
789+
pub const fn mul_f32(self, rhs: f32) -> Duration {
786790
Duration::from_secs_f32(rhs * self.as_secs_f32())
787791
}
788792

@@ -802,7 +806,8 @@ impl Duration {
802806
/// ```
803807
#[stable(feature = "duration_float", since = "1.38.0")]
804808
#[inline]
805-
pub fn div_f64(self, rhs: f64) -> Duration {
809+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
810+
pub const fn div_f64(self, rhs: f64) -> Duration {
806811
Duration::from_secs_f64(self.as_secs_f64() / rhs)
807812
}
808813

@@ -824,7 +829,8 @@ impl Duration {
824829
/// ```
825830
#[stable(feature = "duration_float", since = "1.38.0")]
826831
#[inline]
827-
pub fn div_f32(self, rhs: f32) -> Duration {
832+
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
833+
pub const fn div_f32(self, rhs: f32) -> Duration {
828834
Duration::from_secs_f32(self.as_secs_f32() / rhs)
829835
}
830836

library/core/tests/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
#![feature(core_private_diy_float)]
1111
#![feature(debug_non_exhaustive)]
1212
#![feature(dec2flt)]
13+
#![feature(div_duration)]
14+
#![feature(duration_consts_2)]
1315
#![feature(duration_constants)]
1416
#![feature(duration_saturating_ops)]
17+
#![feature(duration_zero)]
1518
#![feature(exact_size_is_empty)]
1619
#![feature(fixed_size_array)]
1720
#![feature(flt2dec)]

library/core/tests/time.rs

+101
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,104 @@ fn debug_formatting_precision_high() {
321321
assert_eq!(format!("{:.10?}", Duration::new(4, 001_000_000)), "4.0010000000s");
322322
assert_eq!(format!("{:.20?}", Duration::new(4, 001_000_000)), "4.00100000000000000000s");
323323
}
324+
325+
#[test]
326+
fn duration_const() {
327+
// test that the methods of `Duration` are usable in a const context
328+
329+
const DURATION: Duration = Duration::new(0, 123_456_789);
330+
331+
const SUB_SEC_MILLIS: u32 = DURATION.subsec_millis();
332+
assert_eq!(SUB_SEC_MILLIS, 123);
333+
334+
const SUB_SEC_MICROS: u32 = DURATION.subsec_micros();
335+
assert_eq!(SUB_SEC_MICROS, 123_456);
336+
337+
const SUB_SEC_NANOS: u32 = DURATION.subsec_nanos();
338+
assert_eq!(SUB_SEC_NANOS, 123_456_789);
339+
340+
const ZERO: Duration = Duration::zero();
341+
assert_eq!(ZERO, Duration::new(0, 0));
342+
343+
const IS_ZERO: bool = ZERO.is_zero();
344+
assert!(IS_ZERO);
345+
346+
const ONE: Duration = Duration::new(1, 0);
347+
348+
const SECONDS: u64 = ONE.as_secs();
349+
assert_eq!(SECONDS, 1);
350+
351+
const FROM_SECONDS: Duration = Duration::from_secs(1);
352+
assert_eq!(FROM_SECONDS, ONE);
353+
354+
const SECONDS_F32: f32 = ONE.as_secs_f32();
355+
assert_eq!(SECONDS_F32, 1.0);
356+
357+
const FROM_SECONDS_F32: Duration = Duration::from_secs_f32(1.0);
358+
assert_eq!(FROM_SECONDS_F32, ONE);
359+
360+
const SECONDS_F64: f64 = ONE.as_secs_f64();
361+
assert_eq!(SECONDS_F64, 1.0);
362+
363+
const FROM_SECONDS_F64: Duration = Duration::from_secs_f64(1.0);
364+
assert_eq!(FROM_SECONDS_F64, ONE);
365+
366+
const MILLIS: u128 = ONE.as_millis();
367+
assert_eq!(MILLIS, 1_000);
368+
369+
const FROM_MILLIS: Duration = Duration::from_millis(1_000);
370+
assert_eq!(FROM_MILLIS, ONE);
371+
372+
const MICROS: u128 = ONE.as_micros();
373+
assert_eq!(MICROS, 1_000_000);
374+
375+
const FROM_MICROS: Duration = Duration::from_micros(1_000_000);
376+
assert_eq!(FROM_MICROS, ONE);
377+
378+
const NANOS: u128 = ONE.as_nanos();
379+
assert_eq!(NANOS, 1_000_000_000);
380+
381+
const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000);
382+
assert_eq!(FROM_NANOS, ONE);
383+
384+
const MAX: Duration = Duration::new(u64::MAX, 999_999_999);
385+
386+
const CHECKED_ADD: Option<Duration> = MAX.checked_add(ONE);
387+
assert_eq!(CHECKED_ADD, None);
388+
389+
const CHECKED_SUB: Option<Duration> = ZERO.checked_sub(ONE);
390+
assert_eq!(CHECKED_SUB, None);
391+
392+
const CHECKED_MUL: Option<Duration> = ONE.checked_mul(1);
393+
assert_eq!(CHECKED_MUL, Some(ONE));
394+
395+
const MUL_F32: Duration = ONE.mul_f32(1.0);
396+
assert_eq!(MUL_F32, ONE);
397+
398+
const MUL_F64: Duration = ONE.mul_f64(1.0);
399+
assert_eq!(MUL_F64, ONE);
400+
401+
const CHECKED_DIV: Option<Duration> = ONE.checked_div(1);
402+
assert_eq!(CHECKED_DIV, Some(ONE));
403+
404+
const DIV_F32: Duration = ONE.div_f32(1.0);
405+
assert_eq!(DIV_F32, ONE);
406+
407+
const DIV_F64: Duration = ONE.div_f64(1.0);
408+
assert_eq!(DIV_F64, ONE);
409+
410+
const DIV_DURATION_F32: f32 = ONE.div_duration_f32(ONE);
411+
assert_eq!(DIV_DURATION_F32, 1.0);
412+
413+
const DIV_DURATION_F64: f64 = ONE.div_duration_f64(ONE);
414+
assert_eq!(DIV_DURATION_F64, 1.0);
415+
416+
const SATURATING_ADD: Duration = MAX.saturating_add(ONE);
417+
assert_eq!(SATURATING_ADD, MAX);
418+
419+
const SATURATING_SUB: Duration = ZERO.saturating_sub(ONE);
420+
assert_eq!(SATURATING_SUB, ZERO);
421+
422+
const SATURATING_MUL: Duration = MAX.saturating_mul(2);
423+
assert_eq!(SATURATING_MUL, MAX);
424+
}

src/test/ui/consts/duration-consts-2.rs

-67
This file was deleted.

0 commit comments

Comments
 (0)