use core::{
cmp::Eq,
fmt::Debug,
hash::Hash,
ops::{Add, AddAssign, Sub, SubAssign},
time::Duration,
};
pub trait Timing
where
Self: Add<Duration, Output = Self>
+ Sub<Duration, Output = Self>
+ AddAssign<Duration>
+ SubAssign<Duration>
+ Unpin
+ Clone
+ PartialEq
+ Eq
+ Ord
+ PartialOrd
+ Hash
+ Debug,
{
fn now() -> Self;
fn duration_since(&self, other: &Self) -> Duration;
#[cfg(all(feature = "time-metric", feature = "unstable"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "time-metric", feature = "unstable"))))]
fn as_secs_f64(&self) -> f64;
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use std::time::Instant as Clock;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Timing for Clock {
fn now() -> Self {
std::time::Instant::now()
}
fn duration_since(&self, other: &Self) -> Duration {
self.duration_since(other.clone())
}
#[cfg(all(feature = "time-metric", feature = "unstable"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "time-metric", feature = "unstable"))))]
fn as_secs_f64(&self) -> f64 {
use core::mem::transmute;
const NSEC_PER_SEC: f64 = 1_000_000_000.0;
let (secs, frac) = unsafe { transmute::<Self, (i64, i64)>(*self) };
secs as f64 + frac as f64 / NSEC_PER_SEC
}
}