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-04 02:29:57 +0100
committerLinus Färnstrand <faern@faern.net>2018-12-13 15:25:14 +0100
commit13f0463a195832f10f0f22105ad14e6d6b2eff59 (patch)
treeaa93f5e194b0d3a8d74072cee40bdc0b8cbaa5a6 /src/libstd/sys/cloudabi
parent9fe5cb5342244a716055fa0162e795deabd4985c (diff)
Add checked_add method to Instant time type
Diffstat (limited to 'src/libstd/sys/cloudabi')
-rw-r--r--src/libstd/sys/cloudabi/time.rs19
1 files changed, 6 insertions, 13 deletions
diff --git a/src/libstd/sys/cloudabi/time.rs b/src/libstd/sys/cloudabi/time.rs
index a442d1e4ad7..fdd172a896c 100644
--- a/src/libstd/sys/cloudabi/time.rs
+++ b/src/libstd/sys/cloudabi/time.rs
@@ -21,8 +21,8 @@ pub struct Instant {
 
 fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
     dur.as_secs()
-        .checked_mul(NSEC_PER_SEC)
-        .and_then(|nanos| nanos.checked_add(dur.subsec_nanos() as abi::timestamp))
+        .checked_mul(NSEC_PER_SEC)?
+        .checked_add(dur.subsec_nanos() as abi::timestamp)
 }
 
 pub fn dur2intervals(dur: &Duration) -> abi::timestamp {
@@ -47,12 +47,10 @@ impl Instant {
         Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)
     }
 
-    pub fn add_duration(&self, other: &Duration) -> Instant {
-        Instant {
-            t: self.t
-                .checked_add(dur2intervals(other))
-                .expect("overflow when adding duration to instant"),
-        }
+    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
+        checked_dur2intervals(other)?
+            .checked_add(self.t)
+            .map(|t| Instant {t})
     }
 
     pub fn sub_duration(&self, other: &Duration) -> Instant {
@@ -95,11 +93,6 @@ impl SystemTime {
         }
     }
 
-    pub fn add_duration(&self, other: &Duration) -> SystemTime {
-        self.checked_add_duration(other)
-            .expect("overflow when adding duration to instant")
-    }
-
     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
         checked_dur2intervals(other)
             .and_then(|d| self.t.checked_add(d))