pub trait Timer {
type Instant: Copy + core::fmt::Debug;
fn now(&self) -> Self::Instant;
fn elapsed(&self, start: Self::Instant) -> core::time::Duration;
}
#[cfg(all(feature = "std", not(feature = "ariel-os"), not(feature = "embassy")))]
pub type DefaultTimer = StdTimer;
#[cfg(feature = "ariel-os")]
pub type DefaultTimer = ArielOsTimer;
#[cfg(feature = "embassy")]
pub type DefaultTimer = EmbassyTimer;
#[cfg(all(feature = "std", not(feature = "ariel-os"), not(feature = "embassy")))]
#[derive(Debug, Clone, Copy, Default)]
pub struct StdTimer;
#[cfg(all(feature = "std", not(feature = "ariel-os"), not(feature = "embassy")))]
extern crate std;
#[cfg(all(feature = "std", not(feature = "ariel-os"), not(feature = "embassy")))]
impl Timer for StdTimer {
type Instant = std::time::Instant;
fn now(&self) -> Self::Instant {
std::time::Instant::now()
}
fn elapsed(&self, start: Self::Instant) -> core::time::Duration {
start.elapsed()
}
}
#[cfg(feature = "ariel-os")]
#[derive(Debug, Clone, Copy, Default)]
pub struct ArielOsTimer;
#[cfg(feature = "ariel-os")]
impl Timer for ArielOsTimer {
type Instant = ariel_os::time::Instant;
fn now(&self) -> Self::Instant {
ariel_os::time::Instant::now()
}
fn elapsed(&self, start: Self::Instant) -> core::time::Duration {
let micros = ariel_os::time::Instant::now().as_micros() - start.as_micros();
core::time::Duration::from_micros(micros)
}
}
#[cfg(feature = "embassy")]
#[derive(Debug, Clone, Copy, Default)]
pub struct EmbassyTimer;
#[cfg(feature = "embassy")]
impl Timer for EmbassyTimer {
type Instant = embassy_time::Instant;
fn now(&self) -> Self::Instant {
embassy_time::Instant::now()
}
fn elapsed(&self, start: Self::Instant) -> core::time::Duration {
let micros = embassy_time::Instant::now().as_micros() - start.as_micros();
core::time::Duration::from_micros(micros)
}
}