#[cfg(unix)]
use crate::time::DurationNewUnchecked;
#[cfg(unix)]
use libc::{clock_gettime, timespec, CLOCK_MONOTONIC_RAW};
#[cfg(unix)]
use std::hash::Hash;
use std::time::Duration;
#[cfg(unix)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Instant(Duration);
#[cfg(windows)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Instant(std::time::Instant);
#[cfg(windows)]
impl Instant {
#[inline(always)]
pub fn now() -> Self {
Self(std::time::Instant::now())
}
#[inline(always)]
pub fn elapsed(&self) -> Duration {
self.0.elapsed()
}
}
#[cfg(unix)]
impl Instant {
pub fn now() -> Self {
let mut t = timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe { clock_gettime(CLOCK_MONOTONIC_RAW, &mut t) };
Self(unsafe { Duration::new_unchecked(t.tv_sec as _, t.tv_nsec as _) })
}
pub fn elapsed(&self) -> Duration {
let mut other = timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe { clock_gettime(CLOCK_MONOTONIC_RAW, &mut other) };
let a = unsafe { Duration::new_unchecked(other.tv_sec as _, other.tv_nsec as _) };
a - self.0
}
}