diff options
| author | Linus Färnstrand <faern@faern.net> | 2018-12-10 23:55:53 +0100 |
|---|---|---|
| committer | Linus Färnstrand <faern@faern.net> | 2018-12-13 15:25:14 +0100 |
| commit | f5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2 (patch) | |
| tree | 609e631c35b2ba6db599e74111d9adc02eb57e7d /src/libstd/sys/redox | |
| parent | 13f0463a195832f10f0f22105ad14e6d6b2eff59 (diff) | |
| download | rust-f5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2.tar.gz rust-f5a99c321b14e69e9d9a6ffaac3a5b7b8906c1f2.zip | |
Add checked_sub for Instant and SystemTime
Diffstat (limited to 'src/libstd/sys/redox')
| -rw-r--r-- | src/libstd/sys/redox/time.rs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/libstd/sys/redox/time.rs b/src/libstd/sys/redox/time.rs index 5ba8b780727..cb2eab52211 100644 --- a/src/libstd/sys/redox/time.rs +++ b/src/libstd/sys/redox/time.rs @@ -63,27 +63,25 @@ impl Timespec { }) } - fn sub_duration(&self, other: &Duration) -> Timespec { + fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> { let mut secs = other .as_secs() .try_into() // <- target type would be `i64` .ok() - .and_then(|secs| self.t.tv_sec.checked_sub(secs)) - .expect("overflow when subtracting duration from time"); + .and_then(|secs| self.t.tv_sec.checked_sub(secs))?; // Similar to above, nanos can't overflow. let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32; if nsec < 0 { nsec += NSEC_PER_SEC as i32; - secs = secs.checked_sub(1).expect("overflow when subtracting \ - duration from time"); + secs = secs.checked_sub(1)?; } - Timespec { + Some(Timespec { t: syscall::TimeSpec { tv_sec: secs, tv_nsec: nsec as i32, }, - } + }) } } @@ -147,11 +145,11 @@ impl Instant { } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { - self.t.checked_add_duration(other).map(|t| Instant { t }) + Some(Instant { t: self.t.checked_add_duration(other)? }) } - pub fn sub_duration(&self, other: &Duration) -> Instant { - Instant { t: self.t.sub_duration(other) } + pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { + Some(Instant { t: self.t.checked_sub_duration(other)? }) } } @@ -175,11 +173,11 @@ impl SystemTime { } pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> { - self.t.checked_add_duration(other).map(|t| SystemTime { t }) + Some(SystemTime { t: self.t.checked_add_duration(other)? }) } - pub fn sub_duration(&self, other: &Duration) -> SystemTime { - SystemTime { t: self.t.sub_duration(other) } + pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> { + Some(SystemTime { t: self.t.checked_sub_duration(other)? }) } } |
