dope_core/executor/drive/turn/
modes.rs1use std::{
2 convert::Infallible,
3 mem::{self, MaybeUninit},
4 ops::ControlFlow,
5 pin,
6};
7
8use dope_sys::{
9 engine::Mode,
10 io::reactor::event::{self, Report},
11};
12use o3::{
13 base::cell::{brand, region},
14 mem::slot,
15};
16
17use crate::{
18 driver::{
19 retained::flight,
20 schedule::{self, app},
21 },
22 executor::{self, drive::turn::scope::Core},
23 io::event::Dispatch,
24 topology::lending,
25};
26
27pub struct App<
28 'turn,
29 'app,
30 'd: 'app,
31 D: executor::raw::Layout<'d>,
32 P: lending::raw::Project<App = D> = mem::ManuallyDrop<D>,
33 S = Option<app::Lease<'turn, 'd>>,
34> {
35 pub(crate) stored:
36 pin::Pin<&'app region::Value<brand::Identity<'d>, executor::raw::Stored<P, D::ParkPlan>>>,
37 pub(crate) token: &'turn mut region::Token<brand::Identity<'d>>,
38 pub(crate) state: S,
39}
40
41pub(crate) struct Boot;
42
43impl<'scope, 'app, 'd: 'app, D, P, R, Q, O> Mode<'d, Q, O>
44 for Core<'scope, 'd, App<'_, 'app, 'd, D, P>, R>
45where
46 D: executor::raw::App<'d>,
47 P: lending::raw::Project<App = D>,
48 R: Report<Event = crate::Reported>,
49 O: executor::Startup<Q>,
50{
51 type Event = event::raw::Completion<'d, flight::Complete<'d>>;
52 type Held = ();
53 type Work = schedule::Turn<'scope, 'd>;
54 type Pending<'p>
55 = &'p mut Option<Self::Event>
56 where
57 'd: 'p;
58
59 fn replay<'p>(
60 &mut self,
61 work: Self::Work,
62 pending: &mut Self::Pending<'p>,
63 ) -> ControlFlow<Self::Held>
64 where
65 'd: 'p,
66 {
67 if let Some(event) = pending.take() {
68 let mut output = MaybeUninit::uninit();
69 <Self as Mode<'d, Q, O>>::emit(
70 self,
71 slot::raw::Vacant::new(&mut output).insert(event),
72 work,
73 pending,
74 )
75 } else {
76 ControlFlow::Continue(())
77 }
78 }
79
80 fn emit<'p>(
81 &mut self,
82 event: slot::Occupied<'_, Self::Event>,
83 work: Self::Work,
84 pending: &mut Self::Pending<'p>,
85 ) -> ControlFlow<Self::Held>
86 where
87 'd: 'p,
88 {
89 let app = lending::raw::Project::exclusive(
90 self.mode.token.pin_mut(self.mode.stored).project().app,
91 );
92 let event = match Dispatch::new(event).scoped::<D::Scope>() {
93 Ok(event) => match D::dispatch(app, event, work, &mut *self.driver) {
94 ControlFlow::Continue(()) => return ControlFlow::Continue(()),
95 ControlFlow::Break(event) => event.into_event(),
96 },
97 Err(event) => event.into_event(),
98 };
99 mem::forget(pending.replace(event.into_inner()));
100 ControlFlow::Break(())
101 }
102
103 fn park(&mut self) {
104 self.turn.expire();
105 drop(self.mode.state.take());
106 executor::raw::Flat::run(self);
107 }
108
109 fn observed(observer: &O) -> ControlFlow<()> {
110 observer.observed(executor::Requested::<Q>::new())
111 }
112}
113
114impl<'scope, 'd, R, Q, O> Mode<'d, Q, O> for Core<'scope, 'd, Boot, R>
115where
116 R: Report<Event = crate::Reported>,
117 O: executor::Startup<Q>,
118{
119 type Event = event::raw::Completion<'d, flight::Complete<'d>>;
120 type Held = Infallible;
121 type Work = schedule::Turn<'scope, 'd>;
122 type Pending<'p>
123 = ()
124 where
125 'd: 'p;
126
127 fn replay<'p>(
128 &mut self,
129 _work: Self::Work,
130 _pending: &mut Self::Pending<'p>,
131 ) -> ControlFlow<Self::Held>
132 where
133 'd: 'p,
134 {
135 ControlFlow::Continue(())
136 }
137
138 fn emit<'p>(
139 &mut self,
140 event: slot::Occupied<'_, Self::Event>,
141 _work: Self::Work,
142 _pending: &mut Self::Pending<'p>,
143 ) -> ControlFlow<Self::Held>
144 where
145 'd: 'p,
146 {
147 drop(event);
148 ControlFlow::Continue(())
149 }
150
151 fn park(&mut self) {
152 self.turn.expire();
153 }
154
155 fn observed(observer: &O) -> ControlFlow<()> {
156 observer.observed(executor::Requested::<Q>::new())
157 }
158}
159
160const _: () = assert!(mem::size_of::<Boot>() == 0);