diff --git a/crates/maybenot/src/framework.rs b/crates/maybenot/src/framework.rs index acee552..b91c403 100644 --- a/crates/maybenot/src/framework.rs +++ b/crates/maybenot/src/framework.rs @@ -518,11 +518,10 @@ where // does the machine limit say no, if set? if machine.max_blocking_frac > 0.0 { // TODO: swap to m_block_dur.div_duration_f64() - let f: f64 = m_block_dur.as_micros() as f64 - / self - .current_time - .saturating_duration_since(runtime.machine_start) - .as_micros() as f64; + let f: f64 = m_block_dur.div_duration_f64( + self.current_time + .saturating_duration_since(runtime.machine_start), + ); if f >= machine.max_blocking_frac { return false; } @@ -531,11 +530,10 @@ where // does the framework say no? if self.max_blocking_frac > 0.0 { // TODO: swap to g_block_dur.div_duration_f64() - let f: f64 = g_block_dur.as_micros() as f64 - / self - .current_time - .saturating_duration_since(self.framework_start) - .as_micros() as f64; + let f: f64 = g_block_dur.div_duration_f64( + self.current_time + .saturating_duration_since(self.framework_start), + ); if f >= self.max_blocking_frac { return false; } diff --git a/crates/maybenot/src/time.rs b/crates/maybenot/src/time.rs index 5b1ebc4..99dd579 100644 --- a/crates/maybenot/src/time.rs +++ b/crates/maybenot/src/time.rs @@ -20,11 +20,11 @@ pub trait Duration: Clone + Copy + AddAssign + PartialOrd { /// Creates a new duration from the specified number of microseconds. fn from_micros(micros: u64) -> Self; - /// Returns the total number of whole microseconds contained by this duration. - fn as_micros(&self) -> u64; - /// Returns true if this duration spans no time. fn is_zero(&self) -> bool; + + /// Divide this duration by another Duration and return f64. + fn div_duration_f64(self, rhs: Self) -> f64; } impl Instant for std::time::Instant { @@ -48,12 +48,14 @@ impl Duration for std::time::Duration { } #[inline(always)] - fn as_micros(&self) -> u64 { - self.as_micros() as u64 + fn is_zero(&self) -> bool { + self.is_zero() } #[inline(always)] - fn is_zero(&self) -> bool { - self.is_zero() + fn div_duration_f64(self, rhs: Self) -> f64 { + // TODO: Can be changed to just `self.div_duration_f64(rhs)` when Rust 1.80 has + // been released and we are fine with that being the oldest working Rust version. + self.as_secs_f64() / rhs.as_secs_f64() } }