From 2624c05acf93ca2b35dae7ae3770fd92b5dce4b5 Mon Sep 17 00:00:00 2001 From: Nathaniel Ringo Date: Tue, 9 Jan 2018 14:21:45 -0600 Subject: [PATCH 1/3] Makes the constructors of Duration const fns. --- src/libstd/time/duration.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 15ddb62bab5..e0c90664119 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -73,7 +73,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - pub fn new(secs: u64, nanos: u32) -> Duration { + pub const fn new(secs: u64, nanos: u32) -> Duration { let secs = secs.checked_add((nanos / NANOS_PER_SEC) as u64) .expect("overflow in Duration::new"); let nanos = nanos % NANOS_PER_SEC; @@ -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,7 +112,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - pub fn from_millis(millis: u64) -> Duration { + pub const 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 } @@ -133,7 +133,7 @@ impl Duration { /// ``` #[unstable(feature = "duration_from_micros", issue = "44400")] #[inline] - pub fn from_micros(micros: u64) -> Duration { + pub const 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 } @@ -154,7 +154,7 @@ impl Duration { /// ``` #[unstable(feature = "duration_extras", issue = "46507")] #[inline] - pub fn from_nanos(nanos: u64) -> Duration { + pub const 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 } From 7caf753d4413cb0ae7bc266fd89e56f1d3842857 Mon Sep 17 00:00:00 2001 From: Nathaniel Ringo Date: Tue, 9 Jan 2018 15:09:54 -0600 Subject: [PATCH 2/3] Fixes Duration constructor const fns other than new, reverts new to non-const. --- src/libstd/time/duration.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index e0c90664119..bb30d0d80bd 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -73,7 +73,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - pub const fn new(secs: u64, nanos: u32) -> Duration { + pub fn new(secs: u64, nanos: u32) -> Duration { let secs = secs.checked_add((nanos / NANOS_PER_SEC) as u64) .expect("overflow in Duration::new"); let nanos = nanos % NANOS_PER_SEC; @@ -113,9 +113,10 @@ impl Duration { #[stable(feature = "duration", since = "1.3.0")] #[inline] pub const 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 } + 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. @@ -134,9 +135,10 @@ impl Duration { #[unstable(feature = "duration_from_micros", issue = "44400")] #[inline] pub const 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 } + 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. @@ -155,9 +157,10 @@ impl Duration { #[unstable(feature = "duration_extras", issue = "46507")] #[inline] pub const 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 } + 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`. From c25178c9fd15a5dffdc91d1905e44e6c39e306aa Mon Sep 17 00:00:00 2001 From: Nathaniel Ringo Date: Tue, 9 Jan 2018 15:31:25 -0600 Subject: [PATCH 3/3] Fixes whitespace. --- src/libstd/time/duration.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index bb30d0d80bd..f01a27c84c1 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -114,9 +114,9 @@ impl Duration { #[inline] pub const fn from_millis(millis: u64) -> Duration { Duration { - secs: millis / MILLIS_PER_SEC, - nanos: ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI, - } + 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. @@ -136,9 +136,9 @@ impl Duration { #[inline] pub const fn from_micros(micros: u64) -> Duration { Duration { - secs: micros / MICROS_PER_SEC, - nanos: ((micros % MICROS_PER_SEC) as u32) * NANOS_PER_MICRO, - } + 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. @@ -158,9 +158,9 @@ impl Duration { #[inline] 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, - } + 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`.