use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
pub trait Instant<S>
where
S: Sized,
{
fn elapsed(&self) -> Duration;
fn now() -> Self;
fn duration_since(&self, earlier: UniversalInstant) -> Duration;
fn checked_add(&self, duration: Duration) -> Option<S>;
fn checked_sub(&self, duration: Duration) -> Option<S>;
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UniversalInstant(std::time::Instant);
#[cfg(not(target_arch = "wasm32"))]
impl Instant<Self> for UniversalInstant {
fn now() -> Self {
Self(std::time::Instant::now())
}
fn duration_since(&self, earlier: UniversalInstant) -> Duration {
self.0.duration_since(earlier.0)
}
fn elapsed(&self) -> Duration {
self.0.elapsed()
}
fn checked_add(&self, duration: Duration) -> Option<Self> {
self.0.checked_add(duration).map(Self)
}
fn checked_sub(&self, duration: Duration) -> Option<Self> {
self.0.checked_sub(duration).map(Self)
}
}
#[cfg(target_arch = "wasm32")]
use std::convert::TryInto;
#[cfg(target_arch = "wasm32")]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UniversalInstant(u64);
#[cfg(target_arch = "wasm32")]
impl Instant<Self> for UniversalInstant {
fn now() -> Self {
Self((unsafe { crate::cx_wasm32::performanceNow() } * 1000.0) as u64)
}
fn duration_since(&self, earlier: UniversalInstant) -> Duration {
Duration::from_micros(self.0 - earlier.0)
}
fn elapsed(&self) -> Duration {
Self::now().duration_since(*self)
}
fn checked_add(&self, duration: Duration) -> Option<Self> {
match duration.as_micros().try_into() {
Ok(duration) => self.0.checked_add(duration).map(|i| Self(i)),
Err(_) => None,
}
}
fn checked_sub(&self, duration: Duration) -> Option<Self> {
match duration.as_micros().try_into() {
Ok(duration) => self.0.checked_sub(duration).map(|i| Self(i)),
Err(_) => None,
}
}
}
impl Add<Duration> for UniversalInstant {
type Output = UniversalInstant;
fn add(self, other: Duration) -> UniversalInstant {
self.checked_add(other).unwrap()
}
}
impl Sub<Duration> for UniversalInstant {
type Output = UniversalInstant;
fn sub(self, other: Duration) -> UniversalInstant {
self.checked_sub(other).unwrap()
}
}
impl Sub<UniversalInstant> for UniversalInstant {
type Output = Duration;
fn sub(self, other: UniversalInstant) -> Duration {
self.duration_since(other)
}
}
impl AddAssign<Duration> for UniversalInstant {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}
impl SubAssign<Duration> for UniversalInstant {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}