diff options
| author | Pietro Albini <pietro@pietroalbini.org> | 2019-01-07 16:25:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-07 16:25:40 +0100 |
| commit | bc38143ad217c8dcf79049f178eccd84211d5956 (patch) | |
| tree | fc0e46cc9bd9700b8ca6e1542feb294cbf422532 /src/libcore | |
| parent | 5cfc8458847fead508e8986b8e4cc62218dbc93f (diff) | |
| parent | 8c902b66339f54b40d3033c864675929b2c8a65b (diff) | |
| download | rust-bc38143ad217c8dcf79049f178eccd84211d5956.tar.gz rust-bc38143ad217c8dcf79049f178eccd84211d5956.zip | |
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); ```
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/time.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libcore/time.rs b/src/libcore/time.rs index b12ee0497d2..a751965dffa 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -23,6 +23,22 @@ 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)) as f64; +/// The duration of one second. +#[unstable(feature = "duration_constants", issue = "57391")] +pub const SECOND: Duration = Duration::from_secs(1); + +/// The duration of one millisecond. +#[unstable(feature = "duration_constants", issue = "57391")] +pub const MILLISECOND: Duration = Duration::from_millis(1); + +/// The duration of one microsecond. +#[unstable(feature = "duration_constants", issue = "57391")] +pub const MICROSECOND: Duration = Duration::from_micros(1); + +/// The duration of one nanosecond. +#[unstable(feature = "duration_constants", issue = "57391")] +pub const NANOSECOND: Duration = Duration::from_nanos(1); + /// A `Duration` type to represent a span of time, typically used for system /// timeouts. /// |
