use crate::{private, Duration, Now};
use std::ops::Sub;
pub trait NowExt: Now + private::NowExtSealed {
fn saturating_duration_since(&self, earlier: Self::Instant) -> <Self::Instant as Sub<Self::Instant>>::Output
where
Self::Instant: Sub<Self::Instant, Output: Duration>;
fn saturating_duration_until(&self, later: Self::Instant) -> <Self::Instant as Sub<Self::Instant>>::Output
where
Self::Instant: Sub<Self::Instant, Output: Duration>;
}
impl<T> private::NowExtSealed for T where T: ?Sized + Now {}
impl<T> NowExt for T
where
T: ?Sized + Now,
{
fn saturating_duration_since(&self, earlier: Self::Instant) -> <Self::Instant as Sub<Self::Instant>>::Output
where
Self::Instant: Sub<Self::Instant, Output: Duration>,
{
let now = self.now();
if now <= earlier {
return <<Self::Instant as Sub<_>>::Output as Duration>::zero();
}
let duration = now - earlier;
let zero = <<Self::Instant as Sub<_>>::Output as Duration>::zero();
if duration <= zero {
return zero;
}
duration
}
fn saturating_duration_until(&self, later: Self::Instant) -> <Self::Instant as Sub<Self::Instant>>::Output
where
Self::Instant: Sub<Self::Instant, Output: Duration>,
{
let now = self.now();
if later <= now {
return <<Self::Instant as Sub<_>>::Output as Duration>::zero();
}
let duration = later - now;
let zero = <<Self::Instant as Sub<_>>::Output as Duration>::zero();
if duration <= zero {
return zero;
}
duration
}
}