use super::{Duration, Wait};
use std::future::IntoFuture;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use wasip2::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(wasip2::clocks::monotonic_clock::now())
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
Duration::from_nanos(self.0.saturating_sub(earlier.0))
}
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 IntoFuture for Instant {
type Output = Instant;
type IntoFuture = Wait;
fn into_future(self) -> Self::IntoFuture {
crate::task::sleep_until(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_duration_since() {
let x = Instant::now();
let d = Duration::new(456, 789);
let y = x + d;
assert_eq!(y.duration_since(x), d);
}
}