Skip to main content

dope_runtime/executor/session/
mod.rs

1use std::{mem, result::Result as StdResult, time};
2
3use dope_core::{
4    Errno,
5    driver::{life, schedule::clock},
6    io::Event,
7};
8
9use crate::executor::{InstallError, drive};
10
11mod access;
12mod app;
13mod error;
14mod lent;
15mod manual;
16pub(crate) mod run;
17mod scope;
18
19pub use access::Access;
20pub use app::App;
21pub use error::Error;
22/// Session completion preserving output when quiescence fails.
23pub type Result<'report, T> = std::result::Result<T, Error<'report, T>>;
24
25pub use drive::block::Fixed;
26pub use manual::Manual;
27pub use scope::Scope;
28
29/// Exact runtime failures borrowed independently of retained application output.
30#[derive(Debug)]
31pub enum Cause<'a> {
32    Drive(&'a drive::Error),
33    Quiesce {
34        drive: &'a drive::Error,
35        disarm: Option<Errno>,
36    },
37    Install {
38        cause: &'a InstallError,
39        disarm: Option<Errno>,
40    },
41}
42
43pub(crate) struct Core<'a, 'd, Q> {
44    pub(crate) scope: life::Scope<'a, 'd>,
45    pub(super) pending: Option<Event<'d>>,
46    pub(crate) lifecycle: &'a Q,
47}
48
49impl<'a, 'd, Q> Core<'a, 'd, Q> {
50    pub(super) fn new(scope: life::Scope<'a, 'd>, source: &'a Q) -> Self {
51        Self {
52            scope,
53            pending: None,
54            lifecycle: source,
55        }
56    }
57
58    fn admit(&self, start: Option<clock::Deadline<'d>>) -> StdResult<clock::Sample, drive::Error> {
59        let now = start.map_or_else(time::Instant::now, clock::Deadline::into_raw);
60        self.check_time(now)?;
61        Ok(clock::Sample::new(now))
62    }
63
64    fn check_time(&self, now: time::Instant) -> StdResult<(), drive::Error> {
65        let current = self.scope.driver_ref().clock().now().into_raw();
66        if now < current {
67            return Err(drive::Error::regression(now, current));
68        }
69        Ok(())
70    }
71}
72
73const _: () = {
74    assert!(
75        mem::size_of::<Core<'static, 'static, ()>>()
76            == mem::size_of::<life::Scope<'static, 'static>>()
77                + mem::size_of::<Option<Event<'static>>>()
78                + mem::size_of::<usize>()
79    );
80};
81
82/// Owns exact diagnostics after an application has relinquished its session.
83#[derive(Debug, Default)]
84pub struct Completion {
85    quiesce: Option<drive::Error>,
86}
87
88impl Completion {
89    pub const fn new() -> Self {
90        Self { quiesce: None }
91    }
92
93    pub(crate) fn failed<T>(
94        &mut self,
95        output: T,
96        drive: drive::Error,
97        disarm: Option<Errno>,
98    ) -> Error<'_, T> {
99        let drive = self.quiesce.insert(drive);
100        Error::Quiesce {
101            output,
102            drive,
103            disarm,
104        }
105    }
106}