diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-03-26 09:05:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-26 09:05:48 +0100 |
| commit | e298691ee9c7b8eef5b6ec965f2f6bc6d3f22b2d (patch) | |
| tree | fce2466c1555c97c42037b02c35460b6c8ca3552 /src/libstd/sys | |
| parent | 0616b73c2fbe019e3c9273ab10cdf2ef16a78df1 (diff) | |
| parent | 1ccad16231f58b09f127e679d54162acbc2f0dae (diff) | |
| download | rust-e298691ee9c7b8eef5b6ec965f2f6bc6d3f22b2d.tar.gz rust-e298691ee9c7b8eef5b6ec965f2f6bc6d3f22b2d.zip | |
Rollup merge of #59374 - faern:simplify-checked-duration-since, r=shepmaster
Simplify checked_duration_since This follows the same design as we updated to in #56490. Internally, all the system specific time implementations are checked, no panics. Then the panicking publicly exported API can just call the checked version of itself and make do with a single panic (`expect`) at the top. Since the internal sys implementations are now checked, this gets rid of the extra `if self >= &earlier` check in `checked_duration_since`. Except likely making the generated machine code simpler, it also reduces the algorithm from "Check panic condition -> call possibly panicking method" to just "call non panicking method". Added two test cases: * Edge case: Make sure `checked_duration_since` on two equal `Instant`s produce a zero duration, not a `None`. * Most common/intended usage: Make sure `later.checked_duration_since(earlier)`, returns an expected value.
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/cloudabi/time.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sys/redox/time.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/sgx/time.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/unix/time.rs | 13 | ||||
| -rw-r--r-- | src/libstd/sys/wasm/time.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/windows/time.rs | 8 |
6 files changed, 18 insertions, 25 deletions
diff --git a/src/libstd/sys/cloudabi/time.rs b/src/libstd/sys/cloudabi/time.rs index d7502c61eff..49a234e1158 100644 --- a/src/libstd/sys/cloudabi/time.rs +++ b/src/libstd/sys/cloudabi/time.rs @@ -33,11 +33,9 @@ impl Instant { Instant { t: 0 } } - pub fn sub_instant(&self, other: &Instant) -> Duration { - let diff = self.t - .checked_sub(other.t) - .expect("second instant is later than self"); - Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + let diff = self.t.checked_sub(other.t)?; + Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/redox/time.rs b/src/libstd/sys/redox/time.rs index 9db3e85ca9c..881ad5c0aeb 100644 --- a/src/libstd/sys/redox/time.rs +++ b/src/libstd/sys/redox/time.rs @@ -137,10 +137,8 @@ impl Instant { false } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.t.sub_timespec(&other.t).unwrap_or_else(|_| { - panic!("specified instant was later than self") - }) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.t.sub_timespec(&other.t).ok() } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/sgx/time.rs b/src/libstd/sys/sgx/time.rs index e4f789c3e36..4659f7ba71f 100644 --- a/src/libstd/sys/sgx/time.rs +++ b/src/libstd/sys/sgx/time.rs @@ -14,8 +14,8 @@ impl Instant { Instant(usercalls::insecure_time()) } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.0 - other.0 + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.0.checked_sub(other.0) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/unix/time.rs b/src/libstd/sys/unix/time.rs index cbb0615911a..6b5a89aee7d 100644 --- a/src/libstd/sys/unix/time.rs +++ b/src/libstd/sys/unix/time.rs @@ -149,12 +149,11 @@ mod inner { true } - pub fn sub_instant(&self, other: &Instant) -> Duration { + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + let diff = self.t.checked_sub(other.t)?; let info = info(); - let diff = self.t.checked_sub(other.t) - .expect("second instant is later than self"); let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64); - Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32) + Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32)) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { @@ -285,10 +284,8 @@ mod inner { false // last clause, used so `||` is always trailing above } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.t.sub_timespec(&other.t).unwrap_or_else(|_| { - panic!("specified instant was later than self") - }) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.t.sub_timespec(&other.t).ok() } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/wasm/time.rs b/src/libstd/sys/wasm/time.rs index c1228a1b75e..3f71461eea4 100644 --- a/src/libstd/sys/wasm/time.rs +++ b/src/libstd/sys/wasm/time.rs @@ -22,8 +22,8 @@ impl Instant { false } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.0 - other.0 + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.0.checked_sub(other.0) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index 2c99bca7009..aa53f1194fd 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -49,17 +49,17 @@ impl Instant { Instant { t: Duration::from_secs(0) } } - pub fn sub_instant(&self, other: &Instant) -> Duration { + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { // On windows there's a threshold below which we consider two timestamps // equivalent due to measurement error. For more details + doc link, // check the docs on epsilon. let epsilon = perf_counter::PerformanceCounterInstant::epsilon(); if other.t > self.t && other.t - self.t <= epsilon { - return Duration::new(0, 0) + Some(Duration::new(0, 0)) + } else { + self.t.checked_sub(other.t) } - self.t.checked_sub(other.t) - .expect("specified instant was later than self") } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { |
