use core::ops::Sub;
use core::time::Duration;
use super::js::PERFORMANCE;
#[cfg(target_feature = "atomics")]
use super::js::TIME_ORIGIN;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Instant(Duration);
impl Instant {
#[must_use]
pub fn now() -> Self {
let now = PERFORMANCE.with(|performance| {
let performance = performance
.as_ref()
.expect("`Performance` object not found");
#[cfg(not(target_feature = "atomics"))]
return performance.now();
#[cfg(target_feature = "atomics")]
TIME_ORIGIN.with(|origin| performance.now() + origin)
});
assert!(
now.is_sign_positive(),
"negative `DOMHighResTimeStamp`s are not supported"
);
Self(time_stamp_to_duration(now))
}
#[must_use]
pub fn duration_since(&self, earlier: Self) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}
#[must_use]
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
self.0.checked_sub(earlier.0)
}
#[must_use]
pub fn elapsed(&self) -> Duration {
Self::now() - *self
}
}
impl Sub<Self> for Instant {
type Output = Duration;
fn sub(self, rhs: Self) -> Duration {
self.duration_since(rhs)
}
}
fn time_stamp_to_duration(time_stamp: f64) -> Duration {
let time_stamp = F64(time_stamp);
Duration::from_millis(time_stamp.trunc() as u64)
+ Duration::from_nanos(F64(time_stamp.fract() * 1.0e6).internal_round_ties_even() as u64)
}
#[derive(Clone, Copy)]
struct F64(f64);
impl F64 {
fn trunc(self) -> f64 {
libm::trunc(self.0)
}
fn fract(self) -> f64 {
self.0 - self.trunc()
}
fn internal_round_ties_even(self) -> f64 {
fn check(this: f64) {
debug_assert!(this.is_sign_positive(), "found negative input");
debug_assert!(
{
let exponent: u64 = this.to_bits() >> 52 & 0x7ff;
exponent < 0x3ff + 52
},
"found number with exponent bigger than 51"
);
}
check(self.0);
let one_over_e = 1.0 / f64::EPSILON;
let xplusoneovere = self.0 + one_over_e;
xplusoneovere - one_over_e
}
}