Skip to content

Commit bc38143

Browse files
authored
Rollup merge of #57375 - stjepang:duration-constants, r=joshtriplett
Add duration constants Add constants `SECOND`, `MILLISECOND`, `MICROSECOND`, and `NANOSECOND` to `core::time`. This will make working with durations more ergonomic. Compare: ```rust // Convenient, but deprecated function. thread::sleep_ms(2000); // The current canonical way to sleep for two seconds. thread::sleep(Duration::from_secs(2)); // Sleeping using one of the new constants. thread::sleep(2 * SECOND); ```
2 parents 5cfc845 + 8c902b6 commit bc38143

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/libcore/time.rs

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ const MILLIS_PER_SEC: u64 = 1_000;
2323
const MICROS_PER_SEC: u64 = 1_000_000;
2424
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64;
2525

26+
/// The duration of one second.
27+
#[unstable(feature = "duration_constants", issue = "57391")]
28+
pub const SECOND: Duration = Duration::from_secs(1);
29+
30+
/// The duration of one millisecond.
31+
#[unstable(feature = "duration_constants", issue = "57391")]
32+
pub const MILLISECOND: Duration = Duration::from_millis(1);
33+
34+
/// The duration of one microsecond.
35+
#[unstable(feature = "duration_constants", issue = "57391")]
36+
pub const MICROSECOND: Duration = Duration::from_micros(1);
37+
38+
/// The duration of one nanosecond.
39+
#[unstable(feature = "duration_constants", issue = "57391")]
40+
pub const NANOSECOND: Duration = Duration::from_nanos(1);
41+
2642
/// A `Duration` type to represent a span of time, typically used for system
2743
/// timeouts.
2844
///

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
#![feature(const_cstr_unchecked)]
249249
#![feature(core_intrinsics)]
250250
#![feature(dropck_eyepatch)]
251+
#![feature(duration_constants)]
251252
#![feature(exact_size_is_empty)]
252253
#![feature(external_doc)]
253254
#![feature(fixed_size_array)]

src/libstd/time.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use sys_common::FromInner;
2121
#[stable(feature = "time", since = "1.3.0")]
2222
pub use core::time::Duration;
2323

24+
#[unstable(feature = "duration_constants", issue = "57391")]
25+
pub use core::time::{SECOND, MILLISECOND, MICROSECOND, NANOSECOND};
26+
2427
/// A measurement of a monotonically nondecreasing clock.
2528
/// Opaque and useful only with `Duration`.
2629
///

0 commit comments

Comments
 (0)