use std::prelude::v1::*;
use crate::clock::{Clock, ReasonablyRealtime, Reference};
use crate::nanos::Nanos;
use std::ops::Add;
use std::sync::Arc;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct QuantaClock {
clock: quanta::Clock,
}
impl Default for QuantaClock {
fn default() -> Self {
let clock = quanta::Clock::default();
Self { clock }
}
}
impl Clock for QuantaClock {
type Instant = QuantaInstant;
fn now(&self) -> Self::Instant {
let nowish = self.clock.raw();
QuantaInstant(Nanos::new(self.clock.delta_as_nanos(0, nowish)))
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct QuantaInstant(Nanos);
impl Add<Nanos> for QuantaInstant {
type Output = QuantaInstant;
fn add(self, other: Nanos) -> QuantaInstant {
QuantaInstant(self.0 + other)
}
}
impl Reference for QuantaInstant {
fn duration_since(&self, earlier: Self) -> Nanos {
self.0.duration_since(earlier.0)
}
fn saturating_sub(&self, duration: Nanos) -> Self {
QuantaInstant(self.0.saturating_sub(duration))
}
}
#[derive(Debug, Clone)]
pub struct QuantaUpkeepClock {
clock: quanta::Clock,
_handle: Arc<quanta::Handle>,
reference: quanta::Instant,
}
impl QuantaUpkeepClock {
pub fn from_interval(interval: Duration) -> Result<QuantaUpkeepClock, quanta::Error> {
let builder = quanta::Upkeep::new(interval);
Self::from_builder(builder)
}
pub fn from_builder(builder: quanta::Upkeep) -> Result<QuantaUpkeepClock, quanta::Error> {
let handle = Arc::new(builder.start()?);
let clock = quanta::Clock::default();
let reference = clock.recent();
Ok(QuantaUpkeepClock {
clock,
_handle: handle,
reference,
})
}
}
impl Clock for QuantaUpkeepClock {
type Instant = QuantaInstant;
fn now(&self) -> Self::Instant {
QuantaInstant(Nanos::from(
self.clock
.recent()
.saturating_duration_since(self.reference),
))
}
}
impl ReasonablyRealtime for QuantaClock {}
#[cfg(test)]
mod test {
use super::*;
use crate::clock::{Clock, QuantaClock, QuantaUpkeepClock, Reference};
use crate::nanos::Nanos;
use std::thread;
use std::time::Duration;
#[test]
fn quanta_impls_coverage() {
let one_ns = Nanos::new(1);
let c = QuantaClock::default();
let now = c.now();
assert_ne!(now + one_ns, now);
assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now));
assert_eq!(Nanos::new(0), Reference::duration_since(&now, now + one_ns));
assert_eq!(
Reference::saturating_sub(&(now + Duration::from_nanos(1).into()), one_ns),
now
);
}
#[test]
fn quanta_upkeep_impls_coverage_and_advances() {
let one_ns = Nanos::new(1);
let c = QuantaUpkeepClock::from_interval(Duration::from_millis(50)).unwrap();
let now = c.now();
assert_ne!(now + one_ns, now);
assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now));
assert_eq!(Nanos::new(0), Reference::duration_since(&now, now + one_ns));
assert_eq!(
Reference::saturating_sub(&(now + Duration::from_nanos(1).into()), one_ns),
now
);
let start = c.now();
for i in 0..5 {
thread::sleep(Duration::from_millis(250));
let now = c.now();
assert!(
now > start,
"now={:?} not after start={:?} on iteration={}",
now,
start,
i
);
}
}
}