use std::time::{Duration, Instant};
pub trait Item {
fn timestamp(&self) -> Instant;
fn age(&self, landmark: Instant) -> f64;
fn value(&self) -> f64;
}
impl Item for Instant {
fn timestamp(&self) -> Instant {
*self
}
fn age(&self, landmark: Instant) -> f64 {
self.checked_duration_since(landmark)
.as_ref()
.map(Duration::as_secs_f64)
.unwrap_or_else(|| -1.0 * landmark.duration_since(*self).as_secs_f64())
}
fn value(&self) -> f64 {
f64::NAN
}
}
impl Item for (Instant, f64) {
fn timestamp(&self) -> Instant {
self.0
}
fn age(&self, landmark: Instant) -> f64 {
self.0.age(landmark)
}
fn value(&self) -> f64 {
self.1
}
}
impl<I> Item for &I
where
I: Item,
{
fn timestamp(&self) -> Instant {
(*self).timestamp()
}
fn age(&self, landmark: Instant) -> f64 {
(*self).age(landmark)
}
fn value(&self) -> f64 {
(*self).value()
}
}