Skip to content

Commit 1ccad16

Browse files
committed
Update sys::time impls to have checked_sub_instant
1 parent d56b1fd commit 1ccad16

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

src/libstd/sys/cloudabi/time.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ impl Instant {
3333
Instant { t: 0 }
3434
}
3535

36-
pub fn sub_instant(&self, other: &Instant) -> Duration {
37-
let diff = self.t
38-
.checked_sub(other.t)
39-
.expect("second instant is later than self");
40-
Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)
36+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
37+
let diff = self.t.checked_sub(other.t)?;
38+
Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32))
4139
}
4240

4341
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

src/libstd/sys/redox/time.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ impl Instant {
137137
false
138138
}
139139

140-
pub fn sub_instant(&self, other: &Instant) -> Duration {
141-
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
142-
panic!("specified instant was later than self")
143-
})
140+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
141+
self.t.sub_timespec(&other.t).ok()
144142
}
145143

146144
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

src/libstd/sys/sgx/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ impl Instant {
1414
Instant(usercalls::insecure_time())
1515
}
1616

17-
pub fn sub_instant(&self, other: &Instant) -> Duration {
18-
self.0 - other.0
17+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
18+
self.0.checked_sub(other.0)
1919
}
2020

2121
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

src/libstd/sys/unix/time.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,11 @@ mod inner {
149149
true
150150
}
151151

152-
pub fn sub_instant(&self, other: &Instant) -> Duration {
152+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
153+
let diff = self.t.checked_sub(other.t)?;
153154
let info = info();
154-
let diff = self.t.checked_sub(other.t)
155-
.expect("second instant is later than self");
156155
let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
157-
Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32)
156+
Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32))
158157
}
159158

160159
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
@@ -285,10 +284,8 @@ mod inner {
285284
false // last clause, used so `||` is always trailing above
286285
}
287286

288-
pub fn sub_instant(&self, other: &Instant) -> Duration {
289-
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
290-
panic!("specified instant was later than self")
291-
})
287+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
288+
self.t.sub_timespec(&other.t).ok()
292289
}
293290

294291
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

src/libstd/sys/wasm/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl Instant {
2222
false
2323
}
2424

25-
pub fn sub_instant(&self, other: &Instant) -> Duration {
26-
self.0 - other.0
25+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
26+
self.0.checked_sub(other.0)
2727
}
2828

2929
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

src/libstd/sys/windows/time.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ impl Instant {
4949
Instant { t: Duration::from_secs(0) }
5050
}
5151

52-
pub fn sub_instant(&self, other: &Instant) -> Duration {
52+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
5353
// On windows there's a threshold below which we consider two timestamps
5454
// equivalent due to measurement error. For more details + doc link,
5555
// check the docs on epsilon.
5656
let epsilon =
5757
perf_counter::PerformanceCounterInstant::epsilon();
5858
if other.t > self.t && other.t - self.t <= epsilon {
59-
return Duration::new(0, 0)
59+
Some(Duration::new(0, 0))
60+
} else {
61+
self.t.checked_sub(other.t)
6062
}
61-
self.t.checked_sub(other.t)
62-
.expect("specified instant was later than self")
6363
}
6464

6565
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {

0 commit comments

Comments
 (0)