use time_point::{Duration, TimePoint};
pub trait Timed: MaybeTimed {
fn time(&self) -> TimePoint;
fn set_time(&mut self, new_time: TimePoint);
fn with_time(mut self, new_time: TimePoint) -> Self
where
Self: Sized,
{
self.set_time(new_time);
self
}
fn time_shifted_by(mut self, delta_t: Duration) -> Self
where
Self: Sized,
{
self.set_time(self.time() + delta_t);
self
}
}
pub trait MaybeTimed {
fn maybe_time(&self) -> Option<TimePoint>;
}
impl MaybeTimed for usize {
fn maybe_time(&self) -> Option<TimePoint> {
None
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct TimeCmp<W: Timed>(pub W);
impl<W: Timed> std::cmp::PartialEq<TimePoint> for TimeCmp<W> {
fn eq(&self, other: &TimePoint) -> bool {
return self.0.time().nanos_since_zero == other.nanos_since_zero;
}
fn ne(&self, other: &TimePoint) -> bool {
return self.0.time().nanos_since_zero != other.nanos_since_zero;
}
}
impl<W: Timed> std::cmp::PartialEq<Self> for TimeCmp<W> {
fn eq(&self, other: &Self) -> bool {
return self.0.time().nanos_since_zero == other.0.time().nanos_since_zero;
}
fn ne(&self, other: &Self) -> bool {
return self.0.time().nanos_since_zero != other.0.time().nanos_since_zero;
}
}
impl<W: Timed> std::cmp::Eq for TimeCmp<W> {}
impl<W: Timed> std::cmp::PartialOrd<TimePoint> for TimeCmp<W> {
fn partial_cmp(&self, other: &TimePoint) -> Option<std::cmp::Ordering> {
return self
.0
.time()
.nanos_since_zero
.partial_cmp(&other.nanos_since_zero);
}
}
impl<W: Timed> std::cmp::PartialOrd<Self> for TimeCmp<W> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
return self
.0
.time()
.nanos_since_zero
.partial_cmp(&other.0.time().nanos_since_zero);
}
}
impl<W: Timed> std::cmp::Ord for TimeCmp<W> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
return self
.0
.time()
.nanos_since_zero
.cmp(&other.0.time().nanos_since_zero);
}
}
impl<W: Timed> std::ops::Deref for TimeCmp<W> {
type Target = W;
fn deref(&self) -> &Self::Target {
&self.0
}
}