pub trait Instant {
fn now() -> Self;
fn elapsed_time(&self) -> u64;
const ONE_SEC: u64;
}
#[derive(Debug, Clone)]
pub struct StdInstant(std::time::Instant);
impl Instant for StdInstant {
const ONE_SEC: u64 = 1_000;
fn now() -> Self {
StdInstant(std::time::Instant::now())
}
fn elapsed_time(&self) -> u64 {
let elapsed = self.0.elapsed();
elapsed.as_secs() * Self::ONE_SEC + u64::from(elapsed.subsec_millis())
}
}
#[derive(Debug, Clone)]
pub struct StdInstantMicros(std::time::Instant);
impl Instant for StdInstantMicros {
const ONE_SEC: u64 = 1_000_000;
fn now() -> Self {
StdInstantMicros(std::time::Instant::now())
}
fn elapsed_time(&self) -> u64 {
let elapsed = self.0.elapsed();
elapsed.as_secs() * Self::ONE_SEC + u64::from(elapsed.subsec_micros())
}
}