dope_runtime/executor/drive/
clock.rs1use std::time;
2
3use dope_core::driver::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(crate) 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(crate) trait Time: Copy {
34 fn start(self) -> clock::Sample {
35 clock::Sample::new(self.park())
36 }
37
38 fn park(self) -> time::Instant;
39
40 fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration>;
41}
42
43pub(crate) trait Source: Time {
44 fn sample(self) -> Reading;
45}
46
47pub(crate) trait Expiring: Time {
48 fn tick(self) -> Result<clock::Sample, Elapsed>;
49}
50
51pub(crate) struct Reading {
52 tick: clock::Sample,
53 wall: time::Instant,
54}
55
56#[derive(Clone, Copy)]
57pub(crate) struct Wall;
58
59impl Time for Wall {
60 fn park(self) -> time::Instant {
61 self.sample().wall
62 }
63
64 fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
65 timeout
66 }
67}
68
69impl Source for Wall {
70 fn sample(self) -> Reading {
71 let wall = time::Instant::now();
72 Reading {
73 tick: clock::Sample::new(wall),
74 wall,
75 }
76 }
77}
78
79#[derive(Clone, Copy)]
80struct Fixed<'d> {
81 now: clock::Deadline<'d>,
82}
83
84impl Time for Fixed<'_> {
85 fn park(self) -> time::Instant {
86 self.now.into_raw()
87 }
88
89 fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
90 timeout.filter(time::Duration::is_zero)
91 }
92}
93
94impl Source for Fixed<'_> {
95 fn sample(self) -> Reading {
96 Reading {
97 tick: clock::Sample::new(self.now.into_raw()),
98 wall: time::Instant::now(),
99 }
100 }
101}
102
103pub(crate) fn wall() -> Wall {
104 Wall
105}
106
107pub(crate) fn at<'d>(now: clock::Deadline<'d>) -> impl Source + use<'d> {
108 Fixed { now }
109}
110
111#[derive(Clone, Copy)]
112pub(crate) struct Bound<S, L> {
113 source: S,
114 limit: L,
115}
116
117pub(crate) trait Limit: Copy {
118 fn left(self, wall: time::Instant) -> time::Duration;
119}
120
121#[derive(Clone, Copy)]
122pub(crate) struct Span {
123 started: time::Instant,
124 timeout: time::Duration,
125}
126
127impl Span {
128 fn new(timeout: time::Duration) -> Self {
129 Self {
130 started: time::Instant::now(),
131 timeout,
132 }
133 }
134}
135
136impl Limit for Span {
137 fn left(self, wall: time::Instant) -> time::Duration {
138 self.timeout
139 .saturating_sub(wall.saturating_duration_since(self.started))
140 }
141}
142
143#[derive(Clone, Copy)]
144pub(crate) struct End(time::Instant);
145
146impl End {
147 fn try_new(duration: time::Duration) -> Result<Self, Range> {
148 let origin = time::Instant::now();
149 Range::try_add(origin, duration).map(Self)
150 }
151}
152
153impl Limit for End {
154 fn left(self, wall: time::Instant) -> time::Duration {
155 self.0.saturating_duration_since(wall)
156 }
157}
158
159impl<S, L> Bound<S, L> {
160 const fn new(source: S, limit: L) -> Self {
161 Self { source, limit }
162 }
163}
164
165impl<S> Bound<S, Span> {
166 pub(crate) fn for_duration(source: S, timeout: time::Duration) -> Self {
167 Self::new(source, Span::new(timeout))
168 }
169}
170
171impl<S> Bound<S, End> {
172 pub(crate) fn until(source: S, timeout: time::Duration) -> Result<Self, Range> {
173 let end = End::try_new(timeout)?;
174 Ok(Self::new(source, end))
175 }
176}
177
178impl<S, L> Time for Bound<S, L>
179where
180 S: Source,
181 L: Limit,
182{
183 fn park(self) -> time::Instant {
184 self.source.park()
185 }
186
187 fn limit(self, timeout: Option<time::Duration>) -> Option<time::Duration> {
188 let timeout = self.source.limit(timeout);
189 if timeout.is_some_and(|wait| wait.is_zero()) {
190 return timeout;
191 }
192 let left = self.limit.left(time::Instant::now());
193 Some(timeout.map_or(left, |wait| wait.min(left)))
194 }
195}
196
197impl<S, L> Expiring for Bound<S, L>
198where
199 S: Source,
200 L: Limit,
201{
202 fn tick(self) -> Result<clock::Sample, Elapsed> {
203 let reading = self.source.sample();
204 if self.limit.left(reading.wall).is_zero() {
205 Err(Elapsed)
206 } else {
207 Ok(reading.tick)
208 }
209 }
210}
211
212const _: () = {
213 assert!(std::mem::size_of::<Wall>() == 0);
214 assert!(std::mem::size_of::<Fixed<'_>>() == std::mem::size_of::<clock::Deadline<'_>>());
215 assert!(std::mem::size_of::<Range>() == std::mem::size_of::<(time::Instant, time::Duration)>());
216 assert!(std::mem::size_of::<Bound<(), End>>() == std::mem::size_of::<time::Instant>());
217 assert!(
218 std::mem::size_of::<Bound<(), Span>>()
219 == std::mem::size_of::<(time::Instant, time::Duration)>()
220 );
221};