#[cfg(not(target_arch = "wasm32"))]
pub use std::time::Instant;
#[cfg(target_arch = "wasm32")]
pub use web::Instant;
#[cfg(target_arch = "wasm32")]
mod web {
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(Duration);
impl Instant {
pub fn now() -> Self {
let ms = crate::platform::backend::web::now_ms();
Self(Duration::from_secs_f64(ms.max(0.0) / 1000.0))
}
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.0.saturating_sub(earlier.0)
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
self.saturating_duration_since(earlier)
}
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
self.0.checked_sub(earlier.0)
}
pub fn elapsed(&self) -> Duration {
Instant::now().saturating_duration_since(*self)
}
pub fn checked_add(&self, d: Duration) -> Option<Instant> {
self.0.checked_add(d).map(Instant)
}
pub fn checked_sub(&self, d: Duration) -> Option<Instant> {
self.0.checked_sub(d).map(Instant)
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, d: Duration) -> Instant {
Instant(self.0 + d)
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, d: Duration) {
self.0 += d;
}
}
impl Sub<Duration> for Instant {
type Output = Instant;
fn sub(self, d: Duration) -> Instant {
Instant(self.0.saturating_sub(d))
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, d: Duration) {
self.0 = self.0.saturating_sub(d);
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, other: Instant) -> Duration {
self.saturating_duration_since(other)
}
}
}