use core::fmt::Debug;
use core::ops::{Add, Mul};
use core::time::Duration;
pub trait Instant: Add<Self::Duration, Output = Self> + Eq + Ord + Debug + Copy {
const START: Self;
type Duration: Mul<u32, Output = Self::Duration> + Debug;
fn hertz_to_duration(hertz: u64) -> Self::Duration;
}
impl Instant for Duration {
const START: Self = Duration::from_nanos(0);
type Duration = Duration;
fn hertz_to_duration(hertz: u64) -> Self::Duration {
Duration::from_nanos(1_000_000_000 / hertz)
}
}
#[cfg(feature = "fugit")]
impl<const NOM: u32, const DENOM: u32> Instant for fugit::Instant<u32, NOM, DENOM>
where
Self: Add<fugit::Duration<u32, NOM, DENOM>, Output = Self>,
{
const START: Self = fugit::Instant::<u32, NOM, DENOM>::from_ticks(0);
type Duration = fugit::Duration<u32, NOM, DENOM>;
fn hertz_to_duration(hertz: u64) -> Self::Duration {
fugit::Duration::<u32, NOM, DENOM>::from_ticks(DENOM / hertz as u32)
}
}
#[cfg(feature = "fugit")]
impl<const NOM: u32, const DENOM: u32> Instant for fugit::Instant<u64, NOM, DENOM>
where
Self: Add<fugit::Duration<u64, NOM, DENOM>, Output = Self>,
{
const START: Self = fugit::Instant::<u64, NOM, DENOM>::from_ticks(0);
type Duration = fugit::Duration<u64, NOM, DENOM>;
fn hertz_to_duration(hertz: u64) -> Self::Duration {
fugit::Duration::<u64, NOM, DENOM>::from_ticks(DENOM as u64 / hertz)
}
}
#[cfg(feature = "femtos")]
impl Instant for femtos::Instant {
const START: Self = femtos::Instant::START;
type Duration = femtos::Duration;
fn hertz_to_duration(hertz: u64) -> Self::Duration {
femtos::Duration::from_femtos(1_000_000_000_000_000 / hertz as femtos::Femtos)
}
}
#[cfg(test)]
mod test {}