about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2019-02-16 14:15:45 +0800
committerkennytm <kennytm@gmail.com>2019-02-17 14:52:23 +0800
commit17b07f99e8db546febdb7c1019f40fccb7a90c67 (patch)
tree7b032a40ce34d4637c9e9330be70af832562a104 /src
parentf8ccdeb0d42116a1bb5445860fd2a91d2493b44a (diff)
parent91f67fd1a75d7cc1b1ac5fc957ff30f16d01232e (diff)
downloadrust-17b07f99e8db546febdb7c1019f40fccb7a90c67.tar.gz
rust-17b07f99e8db546febdb7c1019f40fccb7a90c67.zip
Rollup merge of #58395 - vi:checked_duration_since, r=dtolnay
Instant::checked_duration_since
Diffstat (limited to 'src')
-rw-r--r--src/libstd/lib.rs1
-rw-r--r--src/libstd/time.rs60
2 files changed, 61 insertions, 0 deletions
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index 4f900e8cbad..f849daf2079 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 47a963bcca9..e1c2b2b1d4f 100644
--- a/src/libstd/time.rs
+++ b/src/libstd/time.rs
@@ -218,6 +218,52 @@ 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
+    /// #![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.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<Duration> {
+        if self >= &earlier {
+            Some(self.0.sub_instant(&earlier.0))
+        } else {
+            None
+        }
+    }
+
+    /// 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
@@ -627,6 +673,20 @@ mod tests {
     }
 
     #[test]
+    fn checked_instant_duration_nopanic() {
+        let a = Instant::now();
+        let ret = (a - Duration::new(1, 0)).checked_duration_since(a);
+        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();
         let b = SystemTime::now();