Skip to main content

dope_core/executor/session/
app.rs

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