dope_core/topology/lending/raw/
mod.rs1use dope_sys::io::reactor::event::Report;
2use std::{marker, mem, ops::ControlFlow, pin};
3
4use crate::driver::{self, life, life::finish::Finish, schedule};
5use crate::{client, executor, topology::lending};
6use o3::base::cell::region;
7
8mod project;
9mod resume;
10
11#[repr(transparent)]
13pub struct Borrowed<'a, T>(pin::Pin<&'a mut T>);
14
15impl<'loan, O> Borrowed<'loan, O> {
16 pub(crate) const fn new(owner: pin::Pin<&'loan mut O>) -> Self {
17 Self(owner)
18 }
19
20 pub fn root_client<'d>(&self) -> <O::Provider as client::Provider<'d>>::Client<'loan>
21 where
22 'd: 'loan,
23 O: lending::Root<'d>,
24 O::Provider: 'loan,
25 {
26 let provider = lending::Root::provider(self.0.as_ref());
27 client::Provider::provide(client::Context::<'_, 'loan, 'd, _>::new(provider))
28 }
29}
30pub use project::Project;
31pub use resume::Resume;
32
33unsafe impl<'d, A, B> Resume<'d> for crate::client::Pair<'d, A, B>
37where
38 A: lending::Root<'d> + Resume<'d>,
39 B: lending::Root<'d> + Resume<'d>,
40{
41}
42
43#[pin_project::pin_project]
45#[derive(dope_gen::Application)]
46#[dispatcher(core = crate, driver = 'd, lending = true)]
47pub struct Bound<'app, 'd, H, T> {
48 #[dispatcher(state)]
49 driver: marker::PhantomData<driver::Reference<'d>>,
50 #[pin]
51 #[app]
52 head: pin::Pin<&'app mut H>,
53 #[pin]
54 #[app]
55 tail: mem::ManuallyDrop<T>,
56}
57
58impl<'app, 'd, H, T> Bound<'app, 'd, H, T> {
59 pub(crate) const fn new(head: Borrowed<'app, H>, tail: T) -> Self {
60 const {
61 assert!(mem::size_of::<Self>() == mem::size_of::<(pin::Pin<&'app mut H>, T)>());
62 assert!(mem::align_of::<Self>() == mem::align_of::<(pin::Pin<&'app mut H>, T)>());
63 assert!(!mem::needs_drop::<Self>());
64 }
65 Self {
66 driver: marker::PhantomData,
67 head: head.0,
68 tail: mem::ManuallyDrop::new(tail),
69 }
70 }
71
72 pub fn tail(self: pin::Pin<&Self>) -> pin::Pin<&T> {
73 Project::shared(self.project_ref().tail)
74 }
75}
76
77impl<'d, H, T> lending::Root<'d> for Bound<'_, 'd, H, T>
78where
79 T: lending::Root<'d>,
80{
81 type Provider = T::Provider;
82
83 fn provider(self: pin::Pin<&Self>) -> pin::Pin<&Self::Provider> {
84 lending::Root::provider(Project::shared(self.project_ref().tail))
85 }
86}
87
88unsafe impl<'d, H, T> executor::raw::Drain<'d> for Bound<'_, 'd, H, T>
91where
92 Self: executor::raw::App<'d>,
93 T: executor::raw::Drain<'d>,
94{
95 fn begin(
96 self: pin::Pin<&mut Self>,
97 turn: schedule::Turn<'_, 'd>,
98 driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
99 ) {
100 executor::raw::Drain::begin(Project::exclusive(self.project().tail), turn, driver);
101 }
102
103 fn progress(
104 self: pin::Pin<&Self>,
105 region: ®ion::Token<region::Identity<'d>>,
106 state: Option<&mut schedule::Waits<'d>>,
107 ) -> ControlFlow<()> {
108 executor::raw::Drain::progress(self.tail(), region, state)
109 }
110
111 fn force(
112 self: pin::Pin<&mut Self>,
113 turn: schedule::Turn<'_, 'd>,
114 driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
115 ) {
116 executor::raw::Drain::force(Project::exclusive(self.project().tail), turn, driver);
117 }
118}
119
120impl<'parent, 'd, H, T> Bound<'parent, 'd, H, T>
121where
122 H: executor::raw::App<'d>,
123 T: executor::raw::App<'d>,
124{
125 pub(crate) unsafe fn finish_tail(
128 app: pin::Pin<&mut Self>,
129 install: life::Install<'_, 'd>,
130 finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
131 plan: &executor::raw::Flat,
132 ) -> <H as executor::raw::Layout<'d>>::ParkPlan {
133 let app = app.project();
134 let mut tail = app.tail;
135 T::finish(Project::exclusive(tail.as_mut()), install, finish);
136 unsafe { Project::release(tail) };
138 let head = Project::shared(app.head.as_ref());
139 unsafe { plan.resume(head) }
141 }
142}
143
144const _: () = {
145 assert!(mem::size_of::<Bound<'_, '_, (), ()>>() == mem::size_of::<&mut ()>());
146 assert!(mem::align_of::<Bound<'_, '_, (), ()>>() == mem::align_of::<&mut ()>());
147 assert!(!mem::needs_drop::<Bound<'_, '_, (), ()>>());
148};
149
150pub unsafe trait Head<'d>: Sized {
153 type Joined<T: executor::raw::App<'d>>: executor::raw::App<'d>;
154 type Retired;
155 type Time<C: Copy>;
156
157 fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T>;
158
159 unsafe fn retire<T, C: Copy>(
162 app: pin::Pin<&mut Self::Joined<T>>,
163 install: life::Install<'_, 'd>,
164 finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
165 plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
166 source: C,
167 ) -> (Self::Retired, Self::Time<C>)
168 where
169 T: executor::raw::App<'d>;
170}
171
172unsafe impl<'d> Head<'d> for () {
173 type Joined<T: executor::raw::App<'d>> = T;
174 type Retired = ();
175 type Time<C: Copy> = ();
176
177 fn join<T: executor::raw::App<'d>>(self, tail: T) -> T {
178 tail
179 }
180
181 unsafe fn retire<T, C: Copy>(
182 app: pin::Pin<&mut T>,
183 install: life::Install<'_, 'd>,
184 finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
185 _: &mut T::ParkPlan,
186 _: C,
187 ) -> ((), ())
188 where
189 T: executor::raw::App<'d>,
190 {
191 T::finish(app, install, finish);
192 ((), ())
193 }
194}
195
196unsafe impl<'parent, 'd: 'parent, H> Head<'d> for Borrowed<'parent, H>
197where
198 H: Resume<'d>,
199{
200 type Joined<T: executor::raw::App<'d>> = Bound<'parent, 'd, H, T>;
201 type Retired = H::ParkPlan;
202 type Time<C: Copy> = C;
203
204 fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T> {
205 Bound::new(self, tail)
206 }
207
208 unsafe fn retire<T, C: Copy>(
209 app: pin::Pin<&mut Self::Joined<T>>,
210 install: life::Install<'_, 'd>,
211 finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
212 plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
213 source: C,
214 ) -> (Self::Retired, C)
215 where
216 T: executor::raw::App<'d>,
217 {
218 (
219 unsafe { Bound::finish_tail(app, install, finish, plan) },
220 source,
221 )
222 }
223}