use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(std::time::Instant);
impl Instant {
#[inline]
pub fn now() -> Self {
Self(super::clock::now())
}
pub fn from_std(std: std::time::Instant) -> Self {
Self(std)
}
pub fn into_std(self) -> std::time::Instant {
self.0
}
pub fn duration_since(&self, earlier: Self) -> Duration {
self.0.saturating_duration_since(earlier.0)
}
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
self.0.checked_duration_since(earlier.0)
}
pub fn saturating_duration_since(&self, earlier: Self) -> Duration {
self.0.saturating_duration_since(earlier.0)
}
pub fn elapsed(&self) -> Duration {
Self::now().saturating_duration_since(*self)
}
pub fn checked_add(&self, duration: Duration) -> Option<Self> {
self.0.checked_add(duration).map(Self)
}
pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
self.0.checked_sub(duration).map(Self)
}
}
impl From<std::time::Instant> for Instant {
fn from(std: std::time::Instant) -> Self {
Self(std)
}
}
impl From<Instant> for std::time::Instant {
fn from(instant: Instant) -> Self {
instant.0
}
}
impl Add<Duration> for Instant {
type Output = Self;
fn add(self, duration: Duration) -> Self {
Self(self.0 + duration)
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, duration: Duration) {
self.0 += duration;
}
}
impl Sub<Duration> for Instant {
type Output = Self;
fn sub(self, duration: Duration) -> Self {
Self(self.0 - duration)
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, duration: Duration) {
self.0 -= duration;
}
}
impl Sub for Instant {
type Output = Duration;
fn sub(self, earlier: Self) -> Duration {
self.saturating_duration_since(earlier)
}
}
impl fmt::Debug for Instant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}