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/windows | |
| parent | 13f0463a195832f10f0f22105ad14e6d6b2eff59 (diff) | |
Add checked_sub for Instant and SystemTime
Diffstat (limited to 'src/libstd/sys/windows')
| -rw-r--r-- | src/libstd/sys/windows/time.rs | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index cad36627bf3..bb2c97ea149 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -79,16 +79,16 @@ impl Instant { }) } - pub fn sub_duration(&self, other: &Duration) -> Instant { + pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { let freq = frequency() as u64; let t = other.as_secs().checked_mul(freq).and_then(|i| { (self.t as u64).checked_sub(i) }).and_then(|i| { i.checked_sub(mul_div_u64(other.subsec_nanos() as u64, freq, NANOS_PER_SEC)) - }).expect("overflow when subtracting duration from time"); - Instant { + })?; + Some(Instant { t: t as c::LARGE_INTEGER, - } + }) } } @@ -125,15 +125,13 @@ impl SystemTime { } pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> { - checked_dur2intervals(other) - .and_then(|d| self.intervals().checked_add(d)) - .map(|i| SystemTime::from_intervals(i)) + let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?; + Some(SystemTime::from_intervals(intervals)) } - pub fn sub_duration(&self, other: &Duration) -> SystemTime { - let intervals = self.intervals().checked_sub(dur2intervals(other)) - .expect("overflow when subtracting from time"); - SystemTime::from_intervals(intervals) + pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> { + let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?; + Some(SystemTime::from_intervals(intervals)) } } @@ -185,11 +183,6 @@ fn checked_dur2intervals(dur: &Duration) -> Option<i64> { .ok() } -fn dur2intervals(d: &Duration) -> i64 { - checked_dur2intervals(d) - .expect("overflow when converting duration to intervals") -} - fn intervals2dur(intervals: u64) -> Duration { Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32) |
