Update sys::time impls to have checked_sub_instant
This commit is contained in:
parent
d56b1fd0e7
commit
1ccad16231
6 changed files with 18 additions and 25 deletions
|
@ -33,11 +33,9 @@ impl Instant {
|
||||||
Instant { t: 0 }
|
Instant { t: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||||
let diff = self.t
|
let diff = self.t.checked_sub(other.t)?;
|
||||||
.checked_sub(other.t)
|
Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32))
|
||||||
.expect("second instant is later than self");
|
|
||||||
Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
|
@ -137,10 +137,8 @@ impl Instant {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||||
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
|
self.t.sub_timespec(&other.t).ok()
|
||||||
panic!("specified instant was later than self")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
|
@ -14,8 +14,8 @@ impl Instant {
|
||||||
Instant(usercalls::insecure_time())
|
Instant(usercalls::insecure_time())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||||
self.0 - other.0
|
self.0.checked_sub(other.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
|
@ -149,12 +149,11 @@ mod inner {
|
||||||
true
|
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 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);
|
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> {
|
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
|
false // last clause, used so `||` is always trailing above
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||||
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
|
self.t.sub_timespec(&other.t).ok()
|
||||||
panic!("specified instant was later than self")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
|
@ -22,8 +22,8 @@ impl Instant {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||||
self.0 - other.0
|
self.0.checked_sub(other.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
|
@ -49,17 +49,17 @@ impl Instant {
|
||||||
Instant { t: Duration::from_secs(0) }
|
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
|
// On windows there's a threshold below which we consider two timestamps
|
||||||
// equivalent due to measurement error. For more details + doc link,
|
// equivalent due to measurement error. For more details + doc link,
|
||||||
// check the docs on epsilon.
|
// check the docs on epsilon.
|
||||||
let epsilon =
|
let epsilon =
|
||||||
perf_counter::PerformanceCounterInstant::epsilon();
|
perf_counter::PerformanceCounterInstant::epsilon();
|
||||||
if other.t > self.t && other.t - self.t <= 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> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue