1use std::{convert::Infallible, mem, pin};
2
3use o3::{
4 alloc,
5 base::cell::region,
6 buffer::{
7 hold::limits,
8 resident::{Budget, Reuse},
9 },
10};
11
12use crate::{
13 BranchError, driver,
14 driver::{
15 retained::flight,
16 route::{App, lease, queues},
17 schedule::{clock, clock::timer},
18 settings,
19 },
20};
21
22mod failure;
23mod mapping;
24pub mod path;
25mod removal;
26pub mod resident;
27
28pub use failure::Failure;
29pub use mapping::Mapping;
30pub use removal::Removal;
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33#[repr(transparent)]
34pub struct Address(*mut core::ffi::c_void);
35
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37#[repr(transparent)]
38pub struct Bytes(usize);
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41#[repr(transparent)]
42pub struct Count(usize);
43
44impl Address {
45 pub(crate) const fn new(address: *mut core::ffi::c_void) -> Self {
46 Self(address)
47 }
48}
49
50impl Bytes {
51 pub(crate) const fn new(bytes: usize) -> Self {
52 Self(bytes)
53 }
54}
55
56impl Count {
57 pub(crate) const fn new(count: usize) -> Self {
58 Self(count)
59 }
60}
61
62#[derive(Clone, Copy, Eq, PartialEq)]
64#[repr(transparent)]
65pub struct Resident(usize);
66
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
69pub enum Exhausted {
70 Domains { capacity: usize },
71 Retained { index: usize, cause: Reuse },
72}
73
74const _: () = assert!(size_of::<Exhausted>() == size_of::<usize>() * 4);
75
76impl Resident {
77 pub const fn bytes(bytes: usize) -> Self {
78 Self(bytes)
79 }
80
81 pub const fn from_limit(limit: limits::Bound) -> Self {
82 Self(limit.bytes())
83 }
84
85 const fn get(self) -> usize {
86 self.0
87 }
88}
89
90pub trait Path<Root, Leaf>: Copy
92where
93 Root: Plan,
94 Leaf: Plan,
95{
96 fn get<'life, 'p, 'd>(self, root: &'life Root::Output<'p, 'd>) -> &'life Leaf::Output<'p, 'd>
97 where
98 'd: 'p,
99 'p: 'life,
100 'd: 'life,
101 Self: 'd,
102 Root: 'd,
103 Leaf: 'd;
104}
105
106#[repr(transparent)]
112pub struct Context<'a, 'd>(pub(crate) &'a mut driver::Local<'d>);
113
114impl<'a, 'd> Context<'a, 'd> {
115 pub(super) fn new(local: &'a mut driver::Local<'d>) -> Self {
116 Self(local)
117 }
118
119 pub fn driver(&self) -> driver::Reference<'d> {
120 self.0.driver
121 }
122
123 pub fn receive(&self) -> settings::Receive {
125 self.0.driver.layout()
126 }
127
128 pub fn region(&self) -> ®ion::Token<region::Identity<'d>> {
129 &self.0.region.0
130 }
131
132 pub fn state(&self) -> ®ion::Token<region::Identity<'d, 1>> {
133 &self.0.region.1
134 }
135
136 pub fn timer(&self) -> timer::Timer<'d> {
137 self.0.driver.timer()
138 }
139
140 pub fn clock(&self) -> clock::Clock<'_, 'd> {
141 self.0.driver.scheduler().clock()
142 }
143
144 pub fn allocator(&self) -> resident::Alloc<'d> {
146 self.0.resident
147 }
148
149 pub fn budget(&self, cap: Resident) -> Result<Budget<'d>, Exhausted> {
152 self.0.resident.try_budget(cap.get())
153 }
154
155 pub fn reborrow(&mut self) -> Context<'_, 'd> {
157 Context(&mut *self.0)
158 }
159
160 pub fn try_flights<Tags, const GROUPS: usize, B>(
162 self,
163 need: flight::Need<Tags, GROUPS, B>,
164 ) -> Result<flight::Prepared<'a, 'd, Tags, GROUPS, B>, alloc::Error> {
165 let Self(local) = self;
166 flight::raw::prepare(local, need)
167 }
168
169 pub fn try_reserve<B: App>(self) -> Result<lease::Claim<'d, B>, lease::Error> {
170 let Self(local) = self;
171 lease::Claim::try_new(local)
172 }
173
174 pub(in crate::driver) fn release(&mut self, retired: queues::Retired<'d>) {
175 lease::Registry::release(self.0, retired);
176 }
177}
178
179pub trait Plan {
184 type Output<'p, 'd>: 'p
185 where
186 Self: 'd,
187 'd: 'p;
188}
189
190#[repr(transparent)]
192pub struct Loan<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> {
193 value: pin::Pin<&'a S::Output<'p, 'd>>,
194}
195
196impl<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> Loan<'a, 'p, 'd, S> {
197 pub(in crate::driver) fn new(value: pin::Pin<&'a S::Output<'p, 'd>>) -> Self {
198 Self { value }
199 }
200
201 pub fn pinned(&self) -> pin::Pin<&'a S::Output<'p, 'd>> {
205 self.value
206 }
207}
208
209const _: () = {
210 assert!(mem::size_of::<Loan<'static, 'static, 'static, ()>>() == mem::size_of::<usize>());
211 assert!(mem::align_of::<Loan<'static, 'static, 'static, ()>>() == mem::align_of::<usize>());
212};
213
214pub trait Build: Plan {
216 type Error;
217
218 type Store<'d>
219 where
220 Self: 'd;
221
222 type Owned<'d>
223 where
224 Self: 'd;
225
226 fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
227 where
228 Self: 'd;
229
230 fn try_build<'p, 'd>(
231 self,
232 owned: Self::Owned<'d>,
233 store: &'p mut Self::Store<'d>,
234 ctx: Context<'_, 'd>,
235 ) -> Result<Self::Output<'p, 'd>, Self::Error>
236 where
237 Self: 'd,
238 'd: 'p;
239}
240
241pub trait Create: Plan {
243 type Error;
244
245 fn try_build<'p, 'd>(self, ctx: Context<'_, 'd>) -> Result<Self::Output<'p, 'd>, Self::Error>
246 where
247 Self: 'd,
248 'd: 'p;
249}
250
251impl<T: Create> Build for T {
252 type Owned<'d>
253 = ()
254 where
255 Self: 'd;
256 type Error = <T as Create>::Error;
257 type Store<'d>
258 = ()
259 where
260 Self: 'd;
261
262 fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
263 where
264 Self: 'd,
265 {
266 Ok(((), ()))
267 }
268
269 fn try_build<'p, 'd>(
270 self,
271 _: Self::Owned<'d>,
272 _: &'p mut Self::Store<'d>,
273 ctx: Context<'_, 'd>,
274 ) -> Result<Self::Output<'p, 'd>, Self::Error>
275 where
276 Self: 'd,
277 'd: 'p,
278 {
279 Create::try_build(self, ctx)
280 }
281}
282
283pub trait Claimless: Plan {}
285
286#[repr(transparent)]
287pub struct Value<T>(T);
288
289impl<T> Value<T> {
290 pub const fn new(value: T) -> Self {
291 Self(value)
292 }
293}
294
295impl<T> Plan for Value<T> {
296 type Output<'p, 'd>
297 = T
298 where
299 'd: 'p,
300 Self: 'd;
301}
302
303impl<T> Claimless for Value<T> {}
304
305impl<T> Create for Value<T> {
306 type Error = Infallible;
307
308 fn try_build<'p, 'd>(self, _: Context<'_, 'd>) -> Result<Self::Output<'p, 'd>, Self::Error>
309 where
310 'd: 'p,
311 Self: 'd,
312 {
313 Ok(self.0)
314 }
315}
316
317impl Plan for () {
318 type Output<'p, 'd>
319 = ()
320 where
321 'd: 'p,
322 Self: 'd;
323}
324
325impl Claimless for () {}
326
327impl Create for () {
328 type Error = Infallible;
329
330 fn try_build<'p, 'd>(self, _: Context<'_, 'd>) -> Result<Self::Output<'p, 'd>, Self::Error>
331 where
332 'd: 'p,
333 Self: 'd,
334 {
335 Ok(self)
336 }
337}
338
339impl<A: Plan, B: Plan> Plan for (A, B) {
340 type Output<'p, 'd>
341 = (A::Output<'p, 'd>, B::Output<'p, 'd>)
342 where
343 'd: 'p,
344 Self: 'd;
345}
346
347impl<A: Build, B: Build> Build for (A, B) {
348 type Owned<'d>
349 = (A::Owned<'d>, B::Owned<'d>)
350 where
351 Self: 'd;
352 type Error = BranchError<A::Error, B::Error>;
353 type Store<'d>
354 = (A::Store<'d>, B::Store<'d>)
355 where
356 Self: 'd;
357
358 fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
359 where
360 Self: 'd,
361 {
362 let (first, owned_a) = self.0.try_store().map_err(BranchError::First)?;
363 let (second, owned_b) = self.1.try_store().map_err(BranchError::Second)?;
364 Ok(((first, second), (owned_a, owned_b)))
365 }
366
367 fn try_build<'p, 'd>(
368 self,
369 owned: Self::Owned<'d>,
370 store: &'p mut Self::Store<'d>,
371 mut ctx: Context<'_, 'd>,
372 ) -> Result<Self::Output<'p, 'd>, Self::Error>
373 where
374 'd: 'p,
375 Self: 'd,
376 {
377 let (first, second) = self;
378 let (owned_a, owned_b) = owned;
379 let first = first
380 .try_build(owned_a, &mut store.0, ctx.reborrow())
381 .map_err(BranchError::First)?;
382 let second = second
383 .try_build(owned_b, &mut store.1, ctx)
384 .map_err(BranchError::Second)?;
385 Ok((first, second))
386 }
387}
388
389impl<A: Claimless, B: Claimless> Claimless for (A, B) {}
390
391const _: () = {
392 assert!(std::mem::size_of::<Context<'static, 'static>>() == std::mem::size_of::<usize>());
393 assert!(std::mem::align_of::<Context<'static, 'static>>() == std::mem::align_of::<usize>());
394 assert!(std::mem::size_of::<Value<usize>>() == std::mem::size_of::<usize>());
395 assert!(std::mem::align_of::<Value<usize>>() == std::mem::align_of::<usize>());
396 assert!(std::mem::needs_drop::<Value<String>>() == std::mem::needs_drop::<String>());
397};