diff options
| author | bors <bors@rust-lang.org> | 2018-01-24 10:39:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-01-24 10:39:25 +0000 |
| commit | a0dcecff90c45ad5d4eb60859e22bb3f1b03842a (patch) | |
| tree | fe48d7aca54057127cc2c25bf7fb70558c10a3ea | |
| parent | 9758ff9c0bd812135304385a48135a93372c3ec2 (diff) | |
| parent | c25178c9fd15a5dffdc91d1905e44e6c39e306aa (diff) | |
| download | rust-a0dcecff90c45ad5d4eb60859e22bb3f1b03842a.tar.gz rust-a0dcecff90c45ad5d4eb60859e22bb3f1b03842a.zip | |
Auto merge of #47300 - remexre:duration-constructors-as-const-fns, r=alexcrichton
Makes the constructors of Duration const fns. This affects `Duration::new`, `Duration::from_secs`, `Duration::from_millis`, `Duration::from_micros`, and `Duration::from_nanos`.
| -rw-r--r-- | src/libstd/time/duration.rs | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index cb5bfb9176e..ee444830be4 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -94,7 +94,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - pub fn from_secs(secs: u64) -> Duration { + pub const fn from_secs(secs: u64) -> Duration { Duration { secs: secs, nanos: 0 } } @@ -112,10 +112,11 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - pub fn from_millis(millis: u64) -> Duration { - let secs = millis / MILLIS_PER_SEC; - let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI; - Duration { secs: secs, nanos: nanos } + pub const fn from_millis(millis: u64) -> Duration { + Duration { + secs: millis / MILLIS_PER_SEC, + nanos: ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI, + } } /// Creates a new `Duration` from the specified number of microseconds. @@ -133,10 +134,11 @@ impl Duration { /// ``` #[unstable(feature = "duration_from_micros", issue = "44400")] #[inline] - pub fn from_micros(micros: u64) -> Duration { - let secs = micros / MICROS_PER_SEC; - let nanos = ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO; - Duration { secs: secs, nanos: nanos } + pub const fn from_micros(micros: u64) -> Duration { + Duration { + secs: micros / MICROS_PER_SEC, + nanos: ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO, + } } /// Creates a new `Duration` from the specified number of nanoseconds. @@ -154,10 +156,11 @@ impl Duration { /// ``` #[unstable(feature = "duration_extras", issue = "46507")] #[inline] - pub fn from_nanos(nanos: u64) -> Duration { - let secs = nanos / (NANOS_PER_SEC as u64); - let nanos = (nanos % (NANOS_PER_SEC as u64)) as u32; - Duration { secs: secs, nanos: nanos } + pub const fn from_nanos(nanos: u64) -> Duration { + Duration { + secs: nanos / (NANOS_PER_SEC as u64), + nanos: (nanos % (NANOS_PER_SEC as u64)) as u32, + } } /// Returns the number of _whole_ seconds contained by this `Duration`. |
