1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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(quanta::Clock);
impl Default for QuantaClock {
fn default() -> Self {
QuantaClock(Default::default())
}
}
impl From<quanta::Instant> for Nanos {
fn from(instant: quanta::Instant) -> Self {
instant.as_u64().into()
}
}
impl Clock for QuantaClock {
type Instant = QuantaInstant;
fn now(&self) -> Self::Instant {
QuantaInstant(Nanos::from(self.0.now()))
}
}
#[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(quanta::Clock, Arc<quanta::Handle>);
impl QuantaUpkeepClock {
pub fn from_interval(interval: Duration) -> Result<QuantaUpkeepClock, std::io::Error> {
let builder = quanta::Builder::new(interval);
Self::from_builder(builder)
}
pub fn from_builder(builder: quanta::Builder) -> Result<QuantaUpkeepClock, std::io::Error> {
let handle = builder.start()?;
Ok(QuantaUpkeepClock(
quanta::Clock::default(),
Arc::new(handle),
))
}
}
impl Clock for QuantaUpkeepClock {
type Instant = QuantaInstant;
fn now(&self) -> Self::Instant {
QuantaInstant(Nanos::from(self.0.recent()))
}
}
impl ReasonablyRealtime for QuantaClock {}