Skip to main content

dope_core/executor/drive/
clock.rs

1use std::time;
2
3use crate::driver::{self, schedule::clock};
4
5pub(crate) struct Elapsed;
6
7#[derive(Clone, Copy, Eq, PartialEq, Debug)]
8pub struct Range {
9    origin: time::Instant,
10    duration: time::Duration,
11}
12
13impl Range {
14    pub fn try_add(
15        origin: time::Instant,
16        duration: time::Duration,
17    ) -> Result<time::Instant, Self> {
18        match origin.checked_add(duration) {
19            Some(deadline) => Ok(deadline),
20            None => Err(Self { origin, duration }),
21        }
22    }
23
24    pub const fn origin(self) -> time::Instant {
25        self.origin
26    }
27
28    pub const fn duration(self) -> time::Duration {
29        self.duration
30    }
31}
32
33pub trait Time: Copy {
34    const FIXED: bool;
35    type Proof<'d>;
36
37    fn proof<'d>(self, driver: driver::Reference<'d>) -> Self::Proof<'d>;
38
39    fn start(self) -> clock::Sample {
40        clock::Sample::new(self.park())
41    }
42
43    fn park(self) -> time::Instant;
44
45    fn tick(self) -> Result<clock::Sample, super::Error> {
46        Ok(self.start())
47    }
48
49    fn quiesce(self) -> Result<clock::Sample, super::Error> {
50        Ok(self.start())
51    }
52
53    fn timeout(self, deadline: clock::Deadline<'_>) -> time::Duration {
54        deadline.into_raw().saturating_duration_since(self.park())
55    }
56
57    fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration>;
58}
59
60pub(crate) trait Source: Time {
61    fn sample(self) -> Reading;
62}
63
64pub(crate) struct Reading {
65    tick: clock::Sample,
66    wall: time::Instant,
67}
68
69#[derive(Clone, Copy)]
70pub struct Wall;
71
72impl Time for Wall {
73    const FIXED: bool = false;
74    type Proof<'d> = ();
75
76    fn proof<'d>(self, _driver: driver::Reference<'d>) {}
77
78    fn park(self) -> time::Instant {
79        self.sample().wall
80    }
81
82    fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
83        timeout
84    }
85}
86
87impl Source for Wall {
88    fn sample(self) -> Reading {
89        let wall = time::Instant::now();
90        Reading {
91            tick: clock::Sample::new(wall),
92            wall,
93        }
94    }
95}
96
97#[derive(Clone, Copy)]
98pub struct Fixed<'d> {
99    now: clock::Deadline<'d>,
100}
101
102impl Time for Fixed<'_> {
103    const FIXED: bool = true;
104    type Proof<'d> = clock::Moment<'d>;
105
106    fn proof<'d>(self, driver: driver::Reference<'d>) -> Self::Proof<'d> {
107        driver.clock().now()
108    }
109
110    fn park(self) -> time::Instant {
111        self.now.into_raw()
112    }
113
114    fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
115        timeout.filter(time::Duration::is_zero)
116    }
117}
118
119impl Source for Fixed<'_> {
120    fn sample(self) -> Reading {
121        Reading {
122            tick: clock::Sample::new(self.now.into_raw()),
123            wall: time::Instant::now(),
124        }
125    }
126}
127
128pub(crate) fn wall() -> Wall {
129    Wall
130}
131
132pub(crate) fn at<'d>(now: clock::Deadline<'d>) -> Fixed<'d> {
133    Fixed { now }
134}
135
136#[derive(Clone, Copy)]
137pub struct Bound<S, L> {
138    source: S,
139    limit: L,
140}
141
142pub(crate) trait Limit: Copy {
143    fn left(self, wall: time::Instant) -> time::Duration;
144}
145
146#[derive(Clone, Copy)]
147pub struct Span {
148    started: time::Instant,
149    timeout: time::Duration,
150}
151
152impl Span {
153    fn new(timeout: time::Duration) -> Self {
154        Self {
155            started: time::Instant::now(),
156            timeout,
157        }
158    }
159}
160
161impl Limit for Span {
162    fn left(self, wall: time::Instant) -> time::Duration {
163        self.timeout
164            .saturating_sub(wall.saturating_duration_since(self.started))
165    }
166}
167
168#[derive(Clone, Copy)]
169pub(crate) struct End(time::Instant);
170
171impl End {
172    fn try_new(duration: time::Duration) -> Result<Self, Range> {
173        let origin = time::Instant::now();
174        Range::try_add(origin, duration).map(Self)
175    }
176}
177
178impl Limit for End {
179    fn left(self, wall: time::Instant) -> time::Duration {
180        self.0.saturating_duration_since(wall)
181    }
182}
183
184impl<S, L> Bound<S, L> {
185    const fn new(source: S, limit: L) -> Self {
186        Self { source, limit }
187    }
188
189    pub(crate) fn elapsed(self) -> Result<clock::Sample, Elapsed>
190    where
191        S: Source,
192        L: Limit,
193    {
194        let reading = self.source.sample();
195        if self.limit.left(reading.wall).is_zero() {
196            Err(Elapsed)
197        } else {
198            Ok(reading.tick)
199        }
200    }
201}
202
203impl<S> Bound<S, Span> {
204    pub(crate) fn for_duration(source: S, timeout: time::Duration) -> Self {
205        Self::new(source, Span::new(timeout))
206    }
207}
208
209impl<S> Bound<S, End> {
210    pub(crate) fn until(source: S, timeout: time::Duration) -> Result<Self, Range> {
211        let end = End::try_new(timeout)?;
212        Ok(Self::new(source, end))
213    }
214}
215
216impl<S, L> Time for Bound<S, L>
217where
218    S: Source,
219    L: Limit,
220{
221    const FIXED: bool = S::FIXED;
222    type Proof<'d> = S::Proof<'d>;
223
224    fn proof<'d>(self, driver: driver::Reference<'d>) -> Self::Proof<'d> {
225        self.source.proof(driver)
226    }
227
228    fn park(self) -> time::Instant {
229        self.source.park()
230    }
231
232    fn quiesce(self) -> Result<clock::Sample, super::Error> {
233        self.elapsed().map_err(super::Error::quiesce)
234    }
235
236    fn tick(self) -> Result<clock::Sample, super::Error> {
237        self.elapsed().map_err(super::Error::timeout)
238    }
239
240    fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
241        let timeout = self.source.limit(timeout);
242        if timeout.is_some_and(|wait| wait.is_zero()) {
243            return timeout;
244        }
245        let left = self.limit.left(time::Instant::now());
246        Some(timeout.map_or(left, |wait| wait.min(left)))
247    }
248}
249
250const _: () = {
251    assert!(std::mem::size_of::<Wall>() == 0);
252    assert!(std::mem::size_of::<Fixed<'_>>() == std::mem::size_of::<clock::Deadline<'_>>());
253    assert!(std::mem::size_of::<Range>() == std::mem::size_of::<(time::Instant, time::Duration)>());
254    assert!(std::mem::size_of::<Bound<(), End>>() == std::mem::size_of::<time::Instant>());
255    assert!(
256        std::mem::size_of::<Bound<(), Span>>()
257            == std::mem::size_of::<(time::Instant, time::Duration)>()
258    );
259};