Skip to main content

dope_runtime/executor/session/
app.rs

1use std::{ops, time};
2
3use o3::base::time::Positive;
4
5use crate::{
6    executor::{
7        self, drain,
8        drive::{self, clock},
9        root, session,
10    },
11    shutdown,
12};
13
14/// Live application view scoped to one installed session.
15#[repr(transparent)]
16pub struct App<'app, 'life: 'app, 'd: 'life, D: executor::raw::Layout<'d>, Q = ()> {
17    pub(super) access: session::access::Access<'app, 'life, 'd, D, Q>,
18}
19
20impl<'app, 'life: 'app, 'd: 'life, D, Q> App<'app, 'life, 'd, D, Q>
21where
22    D: executor::raw::App<'d>,
23{
24    pub fn drive<R>(&mut self, root: R) -> Result<R::Output, drive::Error>
25    where
26        R: root::Root<'d>,
27    {
28        self.access
29            .reborrow()
30            .drive(clock::wall(), root, drive::tick)
31    }
32
33    pub fn run(self) -> Result<shutdown::Requested<Q>, drive::Error> {
34        self.access.run()?;
35        Ok(shutdown::Requested::new())
36    }
37
38    /// Drives one root with a wall-clock deadline, checked between executor turns.
39    /// Each root poll must return to the executor for the deadline to be observed.
40    pub fn drive_for<R>(
41        &mut self,
42        timeout: time::Duration,
43        root: R,
44    ) -> Result<R::Output, drive::Error>
45    where
46        R: root::Root<'d>,
47    {
48        let source = clock::Bound::until(clock::wall(), timeout)?;
49        self.access.reborrow().drive(source, root, drive::timeout)
50    }
51
52    /// Serves until shutdown or the wall-clock deadline, checked between turns.
53    /// Each application transition must return to the executor.
54    pub fn run_for(self, timeout: time::Duration) -> Result<shutdown::Requested<Q>, drive::Error> {
55        self.access.run_for(timeout)?;
56        Ok(shutdown::Requested::new())
57    }
58
59    pub fn run_drain(self, timeout: Positive) -> Result<drain::Report<Q>, drive::Error>
60    where
61        D: executor::raw::Drain<'d>,
62    {
63        let outcome = self.access.run_drain(timeout)?;
64        Ok(drain::Report::new(shutdown::Requested::new(), outcome))
65    }
66
67    /// Returns a completed root, or drains after shutdown once the pending root is dropped.
68    /// A root completed in the shutdown turn keeps its output; the drain timeout starts at shutdown.
69    pub fn drive_drain<R>(
70        self,
71        timeout: Positive,
72        root: R,
73    ) -> Result<ops::ControlFlow<R::Output, drain::Report<Q>>, drive::Error>
74    where
75        D: executor::raw::Drain<'d>,
76        R: root::Root<'d>,
77    {
78        Ok(self
79            .access
80            .drive_drain(timeout, root)?
81            .map_continue(|outcome| drain::Report::new(shutdown::Requested::new(), outcome)))
82    }
83}
84
85impl<'app, 'life: 'app, 'd: 'life, D, Q> ops::Deref for App<'app, 'life, 'd, D, Q>
86where
87    D: executor::raw::Layout<'d>,
88{
89    type Target = session::access::Access<'app, 'life, 'd, D, Q>;
90
91    fn deref(&self) -> &Self::Target {
92        &self.access
93    }
94}
95
96impl<'d, D: executor::raw::Layout<'d>, Q> ops::DerefMut for App<'_, '_, 'd, D, Q> {
97    fn deref_mut(&mut self) -> &mut Self::Target {
98        &mut self.access
99    }
100}