-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to run framework with different time sources
Don't hardcode usage of std::time::{Duration, Instant}. Instead make it generic. Still defaults to std time for ergonomics. Should make it possible to use maybenot with for example `coarsetime` crate.
- Loading branch information
Showing
4 changed files
with
110 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::ops::AddAssign; | ||
|
||
/// Trait representing instants in time. Allows using maybenot frameworks with | ||
/// custom time sources. If you want to use maybenot with a different time source | ||
/// than `std::time::Instant`, implement this trait for your instant type, and the | ||
/// [`Duration`] trait for your corresponding duration type. | ||
pub trait Instant: Clone + Copy { | ||
type Duration: Duration; | ||
|
||
/// Returns the amount of time elapsed from another instant to this one. | ||
/// | ||
/// Should return a zero duration if `earlier` is later than `self` | ||
fn duration_since(&self, earlier: Self) -> Self::Duration; | ||
} | ||
|
||
pub trait Duration: Clone + Copy + AddAssign + PartialOrd { | ||
/// Creates a new duration, spanning no time. | ||
fn zero() -> Self; | ||
|
||
/// 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) -> u128; | ||
|
||
/// Returns true if this duration spans no time. | ||
fn is_zero(&self) -> bool; | ||
} | ||
|
||
impl Instant for std::time::Instant { | ||
type Duration = std::time::Duration; | ||
|
||
#[inline(always)] | ||
fn duration_since(&self, earlier: Self) -> Self::Duration { | ||
// Not using `duration_since` directly since it panics on Rust <= 1.59 | ||
// instead of returning a zero time when `earlier` is later than `self` | ||
self.checked_duration_since(earlier).unwrap_or_default() | ||
} | ||
} | ||
|
||
impl Duration for std::time::Duration { | ||
#[inline(always)] | ||
fn zero() -> Self { | ||
Self::ZERO | ||
} | ||
|
||
#[inline(always)] | ||
fn from_micros(micros: u64) -> Self { | ||
Self::from_micros(micros) | ||
} | ||
|
||
#[inline(always)] | ||
fn as_micros(&self) -> u128 { | ||
self.as_micros() | ||
} | ||
|
||
#[inline(always)] | ||
fn is_zero(&self) -> bool { | ||
self.is_zero() | ||
} | ||
} |