use super::{Duration, Wait};
use std::future::IntoFuture;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use wasi::clocks::monotonic_clock;
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Clone, Copy)]
pub struct Instant(pub(crate) monotonic_clock::Instant);
impl Instant {
#[must_use]
pub fn now() -> Self {
Instant(wasi::clocks::monotonic_clock::now())
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
Duration::from_micros(self.0.checked_sub(earlier.0).unwrap_or_default())
}
pub fn elapsed(&self) -> Duration {
Instant::now().duration_since(*self)
}
}
impl Add<Duration> for Instant {
type Output = Self;
fn add(self, rhs: Duration) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, rhs: Duration) {
*self = Self(self.0 + rhs.0)
}
}
impl Sub<Duration> for Instant {
type Output = Self;
fn sub(self, rhs: Duration) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, rhs: Duration) {
*self = Self(self.0 - rhs.0)
}
}
impl std::ops::Deref for Instant {
type Target = monotonic_clock::Instant;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Instant {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl IntoFuture for Instant {
type Output = Instant;
type IntoFuture = Wait;
fn into_future(self) -> Self::IntoFuture {
crate::task::sleep_until(self)
}
}