1use std::{marker, mem, ops::ControlFlow, pin, result::Result as StdResult, time};
2
3use crate::{
4 Errno,
5 driver::{self, life, schedule::clock},
6 io::Event,
7};
8
9use crate::{
10 client,
11 executor::{self, InstallError, drive},
12 topology::lending,
13};
14
15mod access;
16mod app;
17mod error;
18mod lent;
19mod manual;
20pub(crate) mod run;
21
22pub use access::Access;
23pub use app::App;
24pub use error::Error;
25pub type Result<'report, T, X = std::convert::Infallible> =
27 std::result::Result<T, Error<'report, T, X>>;
28
29pub use drive::block::Fixed;
30pub use manual::Manual;
31
32#[derive(Debug)]
34pub enum Cause<'a, X = std::convert::Infallible> {
35 Drive(&'a drive::Error),
36 Quiesce {
37 drive: &'a drive::Error,
38 disarm: Option<Errno>,
39 },
40 Install {
41 cause: &'a InstallError<X>,
42 disarm: Option<Errno>,
43 },
44}
45
46pub(crate) struct Core<'a, 'd, Q, Report> {
47 pub(crate) scope: life::Scope<'a, 'd, Report>,
48 pub(super) pending: Option<Event<'d>>,
49 pub(crate) lifecycle: &'a Q,
50}
51
52pub struct Session<'i, 'life: 'i, 'd: 'life, Q: 'life, Report, H = (), S = (), P: ?Sized + 'i = ()>
53{
54 core: &'i mut Core<'life, 'd, Q, Report>,
55 head: H,
56 phase: S,
57 source: marker::PhantomData<(*mut &'i (), *mut P)>,
58}
59
60enum State<H, E, R, C, X> {
61 Rejected(E),
62 Clock(drive::Error),
63 Executed(H, run::Outcome<R, C, X>, Option<Errno>),
64}
65
66#[must_use]
67pub struct Completed<
68 'i,
69 H,
70 E,
71 R,
72 P: ?Sized = (),
73 C = drive::clock::Wall,
74 X = std::convert::Infallible,
75> {
76 state: State<H, E, R, C, X>,
77 source: marker::PhantomData<(*mut &'i (), *mut P)>,
78}
79
80impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, S, P: ?Sized + 'i, Report: crate::io::Report>
81 Session<'i, 'life, 'd, Q, Report, H, S, P>
82{
83 pub fn driver(&mut self) -> &mut driver::Local<'d> {
84 self.core.scope.context()
85 }
86}
87
88impl<'i, 'life: 'i, 'd: 'life, Q: 'life, Report: crate::io::Report>
89 Session<'i, 'life, 'd, Q, Report>
90{
91 pub fn scope(&mut self) -> &mut life::Scope<'life, 'd, Report> {
92 &mut self.core.scope
93 }
94}
95
96impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, S, P: ?Sized + 'i, Report: crate::io::Report>
97 Session<'i, 'life, 'd, Q, Report, H, S, P>
98where
99 H: lending::raw::Head<'d>,
100{
101 pub fn reject<E, R, C>(self, error: E) -> Completed<'i, H::Retired, E, R, P, C, Q::Error>
102 where
103 Q: executor::Startup,
104 {
105 Completed {
106 state: State::Rejected(error),
107 source: self.source,
108 }
109 }
110}
111
112impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, P: ?Sized + 'i, Report: crate::io::Report>
113 Session<'i, 'life, 'd, Q, Report, H, (), P>
114where
115 H: lending::raw::Head<'d>,
116{
117 pub fn admit(
118 self,
119 start: Option<clock::Deadline<'d>>,
120 ) -> StdResult<
121 Session<'i, 'life, 'd, Q, Report, H, clock::Sample, P>,
122 Session<'i, 'life, 'd, Q, Report, H, drive::Error, P>,
123 > {
124 match self.core.sample(start) {
125 Ok(sample) => Ok(Session {
126 core: self.core,
127 head: self.head,
128 phase: sample,
129 source: self.source,
130 }),
131 Err(error) => Err(Session {
132 core: self.core,
133 head: self.head,
134 phase: error,
135 source: self.source,
136 }),
137 }
138 }
139}
140
141impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, P: ?Sized + 'i, Report: crate::io::Report>
142 Session<'i, 'life, 'd, Q, Report, H, drive::Error, P>
143where
144 H: lending::raw::Head<'d>,
145{
146 pub fn complete<E, R, C>(self) -> Completed<'i, H::Retired, E, R, P, C, Q::Error>
147 where
148 Q: executor::Startup,
149 {
150 Completed {
151 state: State::Clock(self.phase),
152 source: self.source,
153 }
154 }
155}
156
157impl<
158 'i,
159 'life: 'i,
160 'd: 'life,
161 Q: executor::Startup + 'life,
162 H,
163 P: ?Sized + 'i,
164 Report: crate::io::Report,
165> Session<'i, 'life, 'd, Q, Report, H, clock::Sample, P>
166where
167 H: lending::raw::Head<'d>,
168{
169 pub fn with_app<A, E, R>(
170 self,
171 app: A,
172 run: impl for<'run> FnOnce(App<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
173 ) -> Completed<'i, H::Retired, E, R, P, drive::clock::Wall, Q::Error>
174 where
175 A: executor::raw::App<'d>,
176 {
177 let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
178 (run(scope.app()), drive::clock::wall())
179 });
180 Completed {
181 state: State::Executed(head, outcome, disarm),
182 source: self.source,
183 }
184 }
185
186 pub fn drive_until<A, W, E, T>(
187 self,
188 app: A,
189 wait: W,
190 ) -> Completed<
191 'i,
192 H::Retired,
193 E,
194 StdResult<ControlFlow<T, executor::Requested<Q>>, drive::Error>,
195 P,
196 drive::clock::Wall,
197 Q::Error,
198 >
199 where
200 A: executor::raw::App<'d>,
201 W: executor::root::Root<'d>,
202 W::Output: Into<T>,
203 {
204 let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
205 (
206 scope
207 .app()
208 .drive_until(wait)
209 .map(|exit| exit.map_break(Into::into)),
210 drive::clock::wall(),
211 )
212 });
213 Completed {
214 state: State::Executed(head, outcome, disarm),
215 source: self.source,
216 }
217 }
218 pub fn with_quiesce<A, E, R>(
219 self,
220 app: A,
221 timeout: time::Duration,
222 run: impl for<'run> FnOnce(App<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
223 ) -> Completed<
224 'i,
225 H::Retired,
226 E,
227 R,
228 P,
229 drive::clock::Bound<drive::clock::Wall, drive::clock::Span>,
230 Q::Error,
231 >
232 where
233 A: executor::raw::App<'d>,
234 {
235 use drive::clock::{Bound, wall};
236 let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
237 let output = run(scope.app());
238 (output, Bound::for_duration(wall(), timeout))
239 });
240 Completed {
241 state: State::Executed(head, outcome, disarm),
242 source: self.source,
243 }
244 }
245
246 pub fn with_at_for<A, E, R>(
247 self,
248 app: A,
249 timeout: time::Duration,
250 run: impl for<'run> FnOnce(Manual<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
251 ) -> Completed<
252 'i,
253 H::Retired,
254 E,
255 R,
256 P,
257 drive::clock::Bound<drive::clock::Fixed<'d>, drive::clock::Span>,
258 Q::Error,
259 >
260 where
261 A: executor::raw::App<'d>,
262 {
263 use drive::clock::{Bound, at};
264 let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
265 let output = run(Manual {
266 access: Access::new(&mut *scope.core, scope.owner.view()),
267 });
268 let now = scope.core.scope.driver_ref().clock().now().into();
269 (output, Bound::for_duration(at(now), timeout))
270 });
271 Completed {
272 state: State::Executed(head, outcome, disarm),
273 source: self.source,
274 }
275 }
276}
277
278impl<'i, 'life: 'i, 'd: 'life, Q: executor::Startup + 'life, Report: crate::io::Report>
279 Session<'i, 'life, 'd, Q, Report>
280{
281 pub fn as_mut(&mut self) -> Session<'_, 'life, 'd, Q, Report> {
282 Session::new(self.core)
283 }
284
285 pub fn with_app<'report, D, R>(
286 &mut self,
287 report: &'report mut Completion,
288 app: D,
289 run: impl for<'app> FnOnce(App<'app, 'life, 'd, D, Report, Q>) -> R,
290 ) -> Result<'report, R, Q::Error>
291 where
292 D: executor::raw::App<'d>,
293 {
294 let sample = self.core.sample(None).map_err(Error::Drive)?;
295 let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
296 (run(scope.app()), drive::clock::wall())
297 });
298 run::complete(outcome, disarm, report)
299 }
300
301 pub fn with_quiesce<'report, D, R>(
302 &mut self,
303 report: &'report mut Completion,
304 app: D,
305 timeout: time::Duration,
306 run: impl for<'app> FnOnce(App<'app, 'life, 'd, D, Report, Q>) -> R,
307 ) -> Result<'report, R, Q::Error>
308 where
309 D: executor::raw::App<'d>,
310 {
311 use drive::clock::{Bound, wall};
312 let sample = self.core.sample(None).map_err(Error::Drive)?;
313 let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
314 let output = run(scope.app());
315 (output, Bound::for_duration(wall(), timeout))
316 });
317 run::complete(outcome, disarm, report)
318 }
319
320 pub fn with_at_for<'report, D, R>(
321 &mut self,
322 report: &'report mut Completion,
323 app: D,
324 start: clock::Deadline<'d>,
325 timeout: time::Duration,
326 run: impl for<'app> FnOnce(Manual<'app, 'life, 'd, D, Report, Q>) -> R,
327 ) -> Result<'report, R, Q::Error>
328 where
329 D: executor::raw::App<'d>,
330 {
331 use drive::clock::{Bound, at};
332 let sample = self.core.sample(Some(start)).map_err(Error::Drive)?;
333 let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
334 let output = run(Manual {
335 access: Access::new(&mut *scope.core, scope.owner.view()),
336 });
337 let now = scope.core.scope.driver_ref().clock().now().into();
338 (output, Bound::for_duration(at(now), timeout))
339 });
340 run::complete(outcome, disarm, report)
341 }
342
343 pub fn drive<R>(&mut self, root: R) -> StdResult<R::Output, drive::Error>
344 where
345 R: executor::boot::Root<'d>,
346 {
347 use drive::{block, turn};
348 use turn::scope;
349 let observer = self.core.lifecycle;
350 let state = scope::Boot::try_new(self.core).map_err(drive::Error::from);
351 block::run_boot(state, root, observer)
352 }
353
354 pub(crate) fn new(core: &'i mut Core<'life, 'd, Q, Report>) -> Self {
355 Self {
356 core,
357 head: (),
358 phase: (),
359 source: marker::PhantomData,
360 }
361 }
362}
363
364impl<'i, 'life: 'i, 'd: 'life, Q: 'life, O, S, Report: crate::io::Report>
365 Session<'i, 'life, 'd, Q, Report, lending::raw::Borrowed<'i, O>, S, O::Provider>
366where
367 O: lending::Root<'d>,
368 O::Provider: 'i,
369{
370 pub fn root_client(&self) -> <O::Provider as client::Provider<'d>>::Client<'i> {
371 let root = lending::raw::Project::shared(pin::Pin::new(&self.head));
372 let provider = lending::Root::provider(root);
373 client::Provider::provide(client::Context::<'_, 'i, 'd, _>::new(provider))
374 }
375}
376
377impl<'i, 'life: 'i, 'd: 'life, Q: executor::Startup + 'life, S, Report: crate::io::Report>
378 Session<'i, 'life, 'd, Q, Report, (), S>
379{
380 pub fn with_provider<'report, O, E, R, D, C>(
381 self,
382 report: &'report mut Completion,
383 owner: O,
384 body: impl for<'loan> FnOnce(
385 Session<'loan, 'life, 'd, Q, Report, lending::raw::Borrowed<'loan, O>, S, O::Provider>,
386 ) -> (
387 Completed<'loan, O::ParkPlan, E, R, O::Provider, C, Q::Error>,
388 D,
389 ),
390 ) -> (lending::Result<'report, E, R, Q::Error>, D)
391 where
392 O: lending::Root<'d> + lending::raw::Resume<'d>,
393 C: drive::clock::Time,
394 {
395 let mut owner = pin::pin!(owner);
396 let scope = Session {
397 core: &mut *self.core,
398 head: lending::raw::Owner::new(owner.as_mut()),
399 phase: self.phase,
400 source: marker::PhantomData,
401 };
402 let (Completed { state, .. }, data) = body(scope);
403 let result = match state {
404 State::Rejected(error) => Err(lending::Error::Build(error)),
405 State::Clock(error) => Err(lending::Error::Run(Error::Drive(error))),
406 State::Executed(plan, outcome, disarm) => {
407 let head = lending::raw::Owner::new(owner.as_mut());
408 run::finish_parent(self.core, (head, plan), report, outcome, disarm)
409 .map_err(lending::Error::Run)
410 }
411 };
412 (result, data)
413 }
414}
415
416impl<'i, E, R, C, X> Completed<'i, (), E, R, (), C, X> {
417 pub fn finish<'report>(
418 self,
419 report: &'report mut Completion,
420 ) -> lending::Result<'report, E, R, X> {
421 match self.state {
422 State::Rejected(error) => Err(lending::Error::Build(error)),
423 State::Clock(error) => Err(lending::Error::Run(Error::Drive(error))),
424 State::Executed((), outcome, disarm) => {
425 run::complete(outcome, disarm, report).map_err(lending::Error::Run)
426 }
427 }
428 }
429}
430
431impl<'a, 'd, Q: 'a, Report: crate::io::Report> Core<'a, 'd, Q, Report> {
432 pub(super) fn new(scope: life::Scope<'a, 'd, Report>, source: &'a Q) -> Self {
433 Self {
434 scope,
435 pending: None,
436 lifecycle: source,
437 }
438 }
439
440 fn sample(&self, start: Option<clock::Deadline<'d>>) -> StdResult<clock::Sample, drive::Error> {
441 let now = start.map_or_else(time::Instant::now, clock::Deadline::into_raw);
442 self.check_time(now)?;
443 Ok(clock::Sample::new(now))
444 }
445
446 fn check_time(&self, now: time::Instant) -> StdResult<(), drive::Error> {
447 let current = self.scope.driver_ref().clock().now().into_raw();
448 if now < current {
449 return Err(drive::Error::regression(now, current));
450 }
451 Ok(())
452 }
453}
454
455const _: [(); mem::size_of::<Core<'static, 'static, (), ()>>()] =
456 [(); mem::size_of::<life::Scope<'static, 'static, ()>>()
457 + mem::size_of::<Option<Event<'static>>>()
458 + mem::size_of::<usize>()];
459
460#[derive(Debug, Default)]
462pub struct Completion {
463 quiesce: Option<drive::Error>,
464}
465
466impl Completion {
467 pub const fn new() -> Self {
468 Self { quiesce: None }
469 }
470
471 pub(crate) fn failed<T, X>(
472 &mut self,
473 output: T,
474 drive: drive::Error,
475 disarm: Option<Errno>,
476 ) -> Error<'_, T, X> {
477 let drive = self.quiesce.insert(drive);
478 Error::Quiesce {
479 output,
480 drive,
481 disarm,
482 }
483 }
484}