From 7b2a08cf494563c1867200cd994193d1855719f2 Mon Sep 17 00:00:00 2001 From: Vitaly _Vi Shukela Date: Tue, 12 Feb 2019 10:56:26 +0300 Subject: [PATCH 1/3] Add Instant::checked_duration_since, address #58402. --- src/libstd/lib.rs | 1 + src/libstd/time.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 8ecba3ecd68..38fc0bb80d0 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -295,6 +295,7 @@ #![feature(non_exhaustive)] #![feature(alloc_layout_extra)] #![feature(maybe_uninit)] +#![feature(checked_duration_since)] #![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"), feature(global_asm, range_contains, slice_index_methods, decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))] diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 23924559fcc..973edb5479e 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -218,6 +218,30 @@ impl Instant { self.0.sub_instant(&earlier.0) } + /// Returns the amount of time elapsed from another instant to this one, + /// or None if that instant is earlier than this one. + /// + /// # Examples + /// + /// ```no_run + /// use std::time::{Duration, Instant}; + /// use std::thread::sleep; + /// + /// let now = Instant::now(); + /// sleep(Duration::new(1, 0)); + /// let new_now = Instant::now(); + /// println!("{:?}", new_now.checked_duration_since(now)); + /// println!("{:?}", now.checked_duration_since(new_now)); // None + /// ``` + #[unstable(feature = "checked_duration_since", issue = "58402")] + pub fn checked_duration_since(&self, earlier: Instant) -> Option { + if self >= &earlier { + Some(self.0.sub_instant(&earlier.0)) + } else { + None + } + } + /// Returns the amount of time elapsed since this instant was created. /// /// # Panics @@ -626,6 +650,12 @@ mod tests { (a - Duration::new(1, 0)).duration_since(a); } + #[test] + fn checked_instant_duration_nopanic() { + let a = Instant::now(); + (a - Duration::new(1, 0)).checked_duration_since(a); + } + #[test] fn system_time_math() { let a = SystemTime::now(); From d15c35800032e58c17ef1da3577dacc01ef82bb1 Mon Sep 17 00:00:00 2001 From: Vitaly _Vi Shukela Date: Wed, 13 Feb 2019 13:17:33 +0300 Subject: [PATCH 2/3] Fix tests for checked_duration_since --- src/libstd/time.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 973edb5479e..eefb84f697f 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -224,6 +224,7 @@ impl Instant { /// # Examples /// /// ```no_run + /// #![feature(checked_duration_since)] /// use std::time::{Duration, Instant}; /// use std::thread::sleep; /// @@ -653,7 +654,8 @@ mod tests { #[test] fn checked_instant_duration_nopanic() { let a = Instant::now(); - (a - Duration::new(1, 0)).checked_duration_since(a); + let ret = (a - Duration::new(1, 0)).checked_duration_since(a); + assert_eq!(ret, None); } #[test] From 91f67fd1a75d7cc1b1ac5fc957ff30f16d01232e Mon Sep 17 00:00:00 2001 From: Vitaly _Vi Shukela Date: Wed, 13 Feb 2019 15:08:44 +0300 Subject: [PATCH 3/3] Add Instant::saturating_duration_since --- src/libstd/time.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index eefb84f697f..db2630e88d0 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -243,6 +243,27 @@ impl Instant { } } + /// Returns the amount of time elapsed from another instant to this one, + /// or zero duration if that instant is earlier than this one. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(checked_duration_since)] + /// use std::time::{Duration, Instant}; + /// use std::thread::sleep; + /// + /// let now = Instant::now(); + /// sleep(Duration::new(1, 0)); + /// let new_now = Instant::now(); + /// println!("{:?}", new_now.saturating_duration_since(now)); + /// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns + /// ``` + #[unstable(feature = "checked_duration_since", issue = "58402")] + pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { + self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0)) + } + /// Returns the amount of time elapsed since this instant was created. /// /// # Panics @@ -658,6 +679,13 @@ mod tests { assert_eq!(ret, None); } + #[test] + fn saturating_instant_duration_nopanic() { + let a = Instant::now(); + let ret = (a - Duration::new(1, 0)).saturating_duration_since(a); + assert_eq!(ret, Duration::new(0,0)); + } + #[test] fn system_time_math() { let a = SystemTime::now();