Skip to content

Commit

Permalink
Replace as_micros with div_duration_f64
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Jul 19, 2024
1 parent 626c501 commit 1bb17df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 8 additions & 10 deletions crates/maybenot/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions crates/maybenot/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}

0 comments on commit 1bb17df

Please sign in to comment.