about summary refs log tree commit diff
path: root/src/libstd/sys/cloudabi
diff options
context:
space:
mode:
authorLinus Färnstrand <faern@faern.net>2018-12-10 23:55:53 +0100
committerLinus Färnstrand <faern@faern.net>2018-12-13 15:25:14 +0100
commitf5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2 (patch)
tree609e631c35b2ba6db599e74111d9adc02eb57e7d /src/libstd/sys/cloudabi
parent13f0463a195832f10f0f22105ad14e6d6b2eff59 (diff)
downloadrust-f5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2.tar.gz
rust-f5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2.zip
Add checked_sub for Instant and SystemTime
Diffstat (limited to 'src/libstd/sys/cloudabi')
-rw-r--r--src/libstd/sys/cloudabi/time.rs37
1 files changed, 14 insertions, 23 deletions
diff --git a/src/libstd/sys/cloudabi/time.rs b/src/libstd/sys/cloudabi/time.rs
index fdd172a896c..6f023230a27 100644
--- a/src/libstd/sys/cloudabi/time.rs
+++ b/src/libstd/sys/cloudabi/time.rs
@@ -25,11 +25,6 @@ fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
         .checked_add(dur.subsec_nanos() as abi::timestamp)
 }
 
-pub fn dur2intervals(dur: &Duration) -> abi::timestamp {
-    checked_dur2intervals(dur)
-        .expect("overflow converting duration to nanoseconds")
-}
-
 impl Instant {
     pub fn now() -> Instant {
         unsafe {
@@ -48,17 +43,15 @@ impl Instant {
     }
 
     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
-        checked_dur2intervals(other)?
-            .checked_add(self.t)
-            .map(|t| Instant {t})
+        Some(Instant {
+            t: self.t.checked_add(checked_dur2intervals(other)?)?,
+        })
     }
 
-    pub fn sub_duration(&self, other: &Duration) -> Instant {
-        Instant {
-            t: self.t
-                .checked_sub(dur2intervals(other))
-                .expect("overflow when subtracting duration from instant"),
-        }
+    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
+        Some(Instant {
+            t: self.t.checked_sub(checked_dur2intervals(other)?)?,
+        })
     }
 }
 
@@ -94,17 +87,15 @@ impl SystemTime {
     }
 
     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
-        checked_dur2intervals(other)
-            .and_then(|d| self.t.checked_add(d))
-            .map(|t| SystemTime {t})
+        Some(SystemTime {
+            t: self.t.checked_add(checked_dur2intervals(other)?)?,
+        })
     }
 
-    pub fn sub_duration(&self, other: &Duration) -> SystemTime {
-        SystemTime {
-            t: self.t
-                .checked_sub(dur2intervals(other))
-                .expect("overflow when subtracting duration from instant"),
-        }
+    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
+        Some(SystemTime {
+            t: self.t.checked_sub(checked_dur2intervals(other)?)?,
+        })
     }
 }