From 3ff5879f8d50da4807c639a17df5884bcaa8b319 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sat, 30 May 2020 14:55:38 -0400 Subject: [PATCH 1/3] core/time: Add Duration methods for zero This patch adds two methods to `Duration`. The first, `Duration::zero`, provides a `const` constructor for getting an zero-length duration. This is also what `Default` provides (this was clarified in the docs), though `default` is not `const`. The second, `Duration::is_zero`, returns true if a `Duration` spans no time (i.e., because its components are all zero). Previously, the way to do this was either to compare both `as_secs` and `subsec_nanos` to 0, to compare against `Duration::new(0, 0)`, or to use the `u128` method `as_nanos`, none of which were particularly elegant. --- src/libcore/time.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index e2ceaf80c0cda..cf88ff2e10fb7 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -31,7 +31,7 @@ const MICROS_PER_SEC: u64 = 1_000_000; /// the number of nanoseconds. /// /// `Duration`s implement many common traits, including [`Add`], [`Sub`], and other -/// [`ops`] traits. +/// [`ops`] traits. It implements `Default` by returning a zero-length `Duration`. /// /// [`Add`]: ../../std/ops/trait.Add.html /// [`Sub`]: ../../std/ops/trait.Sub.html @@ -223,6 +223,47 @@ impl Duration { } } + /// Creates a new `Duration` that spans no time. + /// + /// # Examples + /// + /// ``` + /// use std::time::Duration; + /// + /// let duration = Duration::zero(); + /// assert!(duration.is_zero()); + /// + /// const IMMEDIATELY: Duration = Duration::zero(); + /// assert!(IMMEDIATELY.is_zero()); + /// ``` + #[unstable(feature = "duration_zero", issue = "none")] + #[inline] + pub const fn zero() -> Duration { + Duration { secs: 0, nanos: 0 } + } + + /// Returns true if this `Duration` spans no time. + /// + /// # Examples + /// + /// ``` + /// use std::time::Duration; + /// + /// assert!(Duration::zero().is_zero()); + /// assert!(Duration::new(0, 0).is_zero()); + /// assert!(Duration::from_nanos(0).is_zero()); + /// assert!(Duration::from_secs(0).is_zero()); + /// + /// assert!(!Duration::new(1, 1).is_zero()); + /// assert!(!Duration::from_nanos(1).is_zero()); + /// assert!(!Duration::from_secs(1).is_zero()); + /// ``` + #[unstable(feature = "duration_zero", issue = "none")] + #[inline] + pub const fn is_zero(&self) -> bool { + self.secs == 0 && self.nanos == 0 + } + /// Returns the number of _whole_ seconds contained by this `Duration`. /// /// The returned value does not include the fractional (nanosecond) part of the From ad7fd6265e62b2170612544caea3b8454ebb3fed Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sat, 30 May 2020 15:20:09 -0400 Subject: [PATCH 2/3] Doctests need feature --- src/libcore/time.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index cf88ff2e10fb7..d94f2378058e7 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -228,6 +228,7 @@ impl Duration { /// # Examples /// /// ``` + /// #![feature(duration_zero)] /// use std::time::Duration; /// /// let duration = Duration::zero(); @@ -247,6 +248,7 @@ impl Duration { /// # Examples /// /// ``` + /// #![feature(duration_zero)] /// use std::time::Duration; /// /// assert!(Duration::zero().is_zero()); From 386114bfd3c8240154a00b00296da9f105bd97ce Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sat, 20 Jun 2020 10:55:39 -0400 Subject: [PATCH 3/3] Revise according to review --- src/libcore/time.rs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index d94f2378058e7..3b6dafeee2540 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -138,6 +138,24 @@ impl Duration { Duration { secs, nanos } } + /// Creates a new `Duration` that spans no time. + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_zero)] + /// use std::time::Duration; + /// + /// let duration = Duration::zero(); + /// assert!(duration.is_zero()); + /// assert_eq!(duration.as_nanos(), 0); + /// ``` + #[unstable(feature = "duration_zero", issue = "73544")] + #[inline] + pub const fn zero() -> Duration { + Duration { secs: 0, nanos: 0 } + } + /// Creates a new `Duration` from the specified number of whole seconds. /// /// # Examples @@ -223,26 +241,6 @@ impl Duration { } } - /// Creates a new `Duration` that spans no time. - /// - /// # Examples - /// - /// ``` - /// #![feature(duration_zero)] - /// use std::time::Duration; - /// - /// let duration = Duration::zero(); - /// assert!(duration.is_zero()); - /// - /// const IMMEDIATELY: Duration = Duration::zero(); - /// assert!(IMMEDIATELY.is_zero()); - /// ``` - #[unstable(feature = "duration_zero", issue = "none")] - #[inline] - pub const fn zero() -> Duration { - Duration { secs: 0, nanos: 0 } - } - /// Returns true if this `Duration` spans no time. /// /// # Examples @@ -260,7 +258,7 @@ impl Duration { /// assert!(!Duration::from_nanos(1).is_zero()); /// assert!(!Duration::from_secs(1).is_zero()); /// ``` - #[unstable(feature = "duration_zero", issue = "none")] + #[unstable(feature = "duration_zero", issue = "73544")] #[inline] pub const fn is_zero(&self) -> bool { self.secs == 0 && self.nanos == 0