Skip to main content

dope_core/executor/session/
access.rs

1use std::{ops::ControlFlow, pin, time};
2
3use drive::{clock, serve, turn};
4use o3::base::{cell::region, time::Positive};
5use turn::scope;
6
7use crate::{
8    client,
9    executor::{self, drain, drive, root, session},
10    topology::lending,
11};
12
13/// Borrowed capabilities shared by every live application view.
14pub struct Access<
15    'app,
16    'life: 'app,
17    'd: 'life,
18    D,
19    Report,
20    Q = (),
21    Plan = <D as executor::raw::Layout<'d>>::ParkPlan,
22> {
23    pub(crate) core: &'app mut session::Core<'life, 'd, Q, Report>,
24    view: executor::raw::View<'app, 'd, D, executor::raw::Live, Plan>,
25}
26
27impl<'app, 'life: 'app, 'd: 'life, D, Q, Plan, Report: crate::io::Report>
28    Access<'app, 'life, 'd, D, Report, Q, Plan>
29{
30    pub(crate) fn reborrow(&mut self) -> Access<'_, 'life, 'd, D, Report, Q, Plan> {
31        Access {
32            core: &mut *self.core,
33            view: self.view,
34        }
35    }
36
37    pub fn state_token(&mut self) -> &mut region::Token<region::Identity<'d, 1>> {
38        &mut self.core.scope.context().regions_mut().1
39    }
40
41    /// Issues the client of the complete installed root.
42    pub fn root_client(&mut self) -> <D::Provider as client::Provider<'d>>::Client<'app>
43    where
44        D: lending::Root<'d>,
45        D::Provider: 'app,
46    {
47        let root = self.view.app(self.core.scope.token());
48        let provider = lending::Root::provider(root);
49        client::Provider::provide(client::Context::new(provider))
50    }
51
52    pub fn client<P>(
53        &mut self,
54        select: impl for<'borrow> FnOnce(pin::Pin<&'borrow D>) -> pin::Pin<&'borrow P>,
55    ) -> <P::Provider as client::Provider<'d>>::Client<'app>
56    where
57        P: lending::Root<'d>,
58        P::Provider: 'app,
59    {
60        let root = select(self.view.app(self.core.scope.token()));
61        let provider = lending::Root::provider(root);
62        client::Provider::provide(client::Context::new(provider))
63    }
64
65    pub fn inspect(&mut self) -> pin::Pin<&D> {
66        self.view.app(self.core.scope.token())
67    }
68}
69
70impl<'app, 'life: 'app, 'd: 'life, D, Q, Plan, Report: crate::io::Report>
71    Access<'app, 'life, 'd, D, Report, Q, Plan>
72where
73    D: executor::raw::App<'d, ParkPlan = Plan>,
74    Q: executor::Startup,
75{
76    pub(super) const fn new(
77        core: &'app mut session::Core<'life, 'd, Q, Report>,
78        view: executor::raw::View<'app, 'd, D, executor::raw::Live, Plan>,
79    ) -> Self {
80        Self { core, view }
81    }
82
83    pub(super) fn drive<R, C>(self, source: C, root: R) -> Result<R::Output, drive::Error>
84    where
85        R: root::Root<'d>,
86        C: clock::Time,
87    {
88        let observer = self.core.lifecycle;
89        self.drive_with(source, root, observer)
90    }
91
92    pub(crate) fn drive_with<R, C, O>(
93        self,
94        source: C,
95        root: R,
96        observer: &O,
97    ) -> Result<R::Output, drive::Error>
98    where
99        R: root::Root<'d>,
100        C: clock::Time,
101        O: executor::Startup<Q>,
102    {
103        let state = self.scope();
104        state.run::<R, C, Q, O>(source, root, observer)
105    }
106
107    pub(super) fn drive_fixed<R, C>(
108        self,
109        source: C,
110        root: R,
111    ) -> Result<session::Fixed<'d, R::Output>, drive::Error>
112    where
113        R: root::Root<'d>,
114        C: clock::Time<Proof<'d> = crate::driver::schedule::clock::Moment<'d>>,
115    {
116        let observer = self.core.lifecycle;
117        let state = self.scope();
118        state.run_fixed(source, root, observer)
119    }
120
121    pub(super) fn run(self) -> Result<(), drive::Error> {
122        let observer = self.core.lifecycle;
123        serve::run(self.scope(), clock::wall(), observer)
124    }
125
126    pub(super) fn run_for(self, timeout: time::Duration) -> Result<(), drive::Error> {
127        let source = clock::Bound::until(clock::wall(), timeout)?;
128        let observer = self.core.lifecycle;
129        serve::run(self.scope(), source, observer)
130    }
131
132    pub(super) fn drive_until<R>(self, root: R) -> Result<ControlFlow<R::Output>, drive::Error>
133    where
134        R: root::Root<'d>,
135    {
136        let observer = self.core.lifecycle;
137        let state = self.scope();
138        state.run_until(clock::wall(), root, observer)
139    }
140
141    pub(super) fn run_drain(mut self, timeout: Positive) -> Result<drain::Outcome, drive::Error>
142    where
143        D: executor::raw::Drain<'d>,
144    {
145        self.reborrow().run()?;
146        self.drain(timeout)
147    }
148
149    pub(super) fn drive_drain<R>(
150        mut self,
151        timeout: Positive,
152        root: R,
153    ) -> Result<ControlFlow<R::Output, drain::Outcome>, drive::Error>
154    where
155        D: executor::raw::Drain<'d>,
156        R: root::Root<'d>,
157    {
158        match self.reborrow().drive_until(root)? {
159            ControlFlow::Break(output) => Ok(ControlFlow::Break(output)),
160            ControlFlow::Continue(()) => self.drain(timeout).map(ControlFlow::Continue),
161        }
162    }
163
164    fn drain(self, timeout: Positive) -> Result<drain::Outcome, drive::Error>
165    where
166        D: executor::raw::Drain<'d>,
167    {
168        let source = clock::Bound::until(clock::wall(), timeout.get())?;
169        let observer = self.core.lifecycle;
170        drain::run(self.scope(), source, observer)
171    }
172
173    fn scope(self) -> scope::Scope<'app, 'app, 'd, D, executor::raw::Live, Report> {
174        scope::Scope::new(&mut self.core.scope, self.view, &mut self.core.pending)
175    }
176}