pub use platform::HailInstant;
#[cfg(windows)]
mod platform {
pub type HailInstant = std::time::Instant;
}
#[cfg(target_os = "linux")]
mod platform {
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct HailInstant(Duration);
impl HailInstant {
pub fn now() -> Self {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
if unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut ts) } != 0 {
panic!("clock_gettime doesn't work!");
}
Self(Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32))
}
pub fn elapsed(&self) -> Duration {
let now = Self::now();
now.0 - self.0
}
}
}
#[cfg(target_os = "macos")]
mod platform {
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct HailInstant(Duration);
extern "C" fn clock_gettime_nsec_np(t: libc::clockid_t) -> u64;
impl HailInstant {
pub fn now() -> Self {
let t = unsafe { clock_gettime_nsec_np(libc::CLOCK_MONOTONIC_RAW) };
Self(Duration::from_nanos(t))
}
pub fn elapsed(&self) -> Duration {
let now = Self::now();
now.0 - self.0
}
}
}