1pub mod indices;
2use core::{convert::Infallible, hash, marker, mem};
3
4pub use erased::Erased;
5use indices::SlotIndex;
6use o3::base::num::Epoch;
7pub(crate) use token::Brand;
8pub use token::{Token, control::Control};
9
10use dope_sys::io::reactor::event::kind::{
11 Accept, Connect, Open, Read, Recv, Send, Socket, Sync, Tuning, Write,
12};
13
14use crate::driver::route;
15
16mod erased;
17pub mod lease;
18pub(in crate::driver) mod queues;
19pub mod raw;
20pub mod table;
21mod token;
22
23pub(crate) const FRAMEWORK: u8 = 255;
24pub(crate) const CAPACITY: usize = u8::MAX as usize + 1;
25
26pub(crate) const SHIFT: u32 = 56;
27pub(crate) const KIND_SHIFT: u32 = 48;
28pub const SLOT_BITS: u32 = 24;
29pub const SLOT_MASK: u64 = (1 << SLOT_BITS) - 1;
30pub(crate) const AUX_SHIFT: u32 = SLOT_BITS;
31pub(crate) const AUX_BITS: u32 = KIND_SHIFT - AUX_SHIFT;
32pub(crate) const AUX_VALUE_MASK: u32 = (1 << AUX_BITS) - 1;
33pub(crate) const AUX_MASK: u64 = (AUX_VALUE_MASK as u64) << AUX_SHIFT;
34
35#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
38#[repr(transparent)]
39pub struct Id(u8);
40
41#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
43#[repr(transparent)]
44pub struct Route(u8);
45
46pub trait Selection: kind::Sealed + Copy {
47 const FIXED: Option<Route>;
48 fn select(route: Route) -> Option<Self>;
49 fn into_route(self) -> Route;
50}
51
52pub trait Scope<R: App>: Selection {}
54
55impl<R: App> Scope<R> for Infallible {}
56impl<R: App> Scope<R> for marker::PhantomData<R> {}
57
58impl kind::Sealed for Route {}
59
60impl Selection for Route {
61 const FIXED: Option<Route> = None;
62
63 fn select(route: Route) -> Option<Self> {
64 Some(route)
65 }
66
67 fn into_route(self) -> Route {
68 self
69 }
70}
71
72impl Selection for Infallible {
73 const FIXED: Option<Route> = None;
74
75 fn select(_: Route) -> Option<Self> {
76 None
77 }
78
79 fn into_route(self) -> Route {
80 match self {}
81 }
82}
83
84impl<R: App> Selection for marker::PhantomData<R> {
85 const FIXED: Option<Route> = Some(R::ROUTE);
86
87 fn select(route: Route) -> Option<Self> {
88 (route == R::ROUTE).then_some(marker::PhantomData)
89 }
90
91 fn into_route(self) -> Route {
92 R::ROUTE
93 }
94}
95
96impl Id {
97 pub(crate) const fn for_tag<Tag: route::Tag>() -> Self {
98 const { assert!(Tag::ROUTE.raw() != FRAMEWORK) };
99 Self(Tag::ROUTE.raw())
100 }
101
102 pub const fn fixed<const ID: u8>() -> Self {
103 const { assert!(ID != FRAMEWORK, "framework route is reserved") };
104 Self(ID)
105 }
106
107 pub const fn raw(self) -> u8 {
109 self.0
110 }
111
112 pub(crate) const fn into_route(self) -> Route {
113 Route(self.0)
114 }
115}
116
117impl Route {
118 pub(crate) const FRAMEWORK: Self = Self(FRAMEWORK);
119
120 pub(crate) const fn app(self) -> Option<Id> {
121 if self.is_framework() {
122 None
123 } else {
124 Some(Id(self.0))
125 }
126 }
127
128 pub(crate) const fn from_bits(raw: u8) -> Self {
129 Self(raw)
130 }
131
132 pub const fn is_framework(self) -> bool {
133 self.0 == FRAMEWORK
134 }
135
136 pub(crate) const fn raw(self) -> u8 {
137 self.0
138 }
139}
140
141impl From<Id> for Route {
142 fn from(id: Id) -> Self {
143 Self(id.raw())
144 }
145}
146
147impl TryFrom<Route> for Id {
148 type Error = Route;
149
150 fn try_from(route: Route) -> Result<Self, Self::Error> {
151 route.app().ok_or(route)
152 }
153}
154
155#[derive(Clone, Copy, Debug)]
159pub struct KeyTag<const ROUTE: u8, C: kind::Class = kind::Any>(marker::PhantomData<C>);
160
161#[derive(Clone, Copy)]
163pub struct FrameworkTag<C: kind::Class>(marker::PhantomData<C>);
164
165pub trait Tag: Copy {
167 const ROUTE: Route;
168 type Class: kind::Class;
169}
170
171pub trait Select<C: kind::Class>: Tag {}
173
174impl<const ROUTE: u8> Select<Accept> for KeyTag<ROUTE, Accept> {}
175impl<const ROUTE: u8> Select<Recv> for KeyTag<ROUTE, Recv> {}
176impl<const ROUTE: u8> Select<Send> for KeyTag<ROUTE, Send> {}
177impl<const ROUTE: u8> Select<Socket> for KeyTag<ROUTE, Socket> {}
178impl<const ROUTE: u8> Select<Connect> for KeyTag<ROUTE, Connect> {}
179impl<const ROUTE: u8> Select<kind::Shutdown> for KeyTag<ROUTE, kind::Shutdown> {}
180impl<const ROUTE: u8> Select<Open> for KeyTag<ROUTE, Open> {}
181impl<const ROUTE: u8> Select<kind::OneShot> for KeyTag<ROUTE, kind::OneShot> {}
182impl<const ROUTE: u8> Select<kind::TaskQueue> for KeyTag<ROUTE, kind::TaskQueue> {}
183impl<const ROUTE: u8> Select<Tuning> for KeyTag<ROUTE, Tuning> {}
184impl<const ROUTE: u8> Select<Write> for KeyTag<ROUTE, Write> {}
185impl<const ROUTE: u8> Select<Sync> for KeyTag<ROUTE, Sync> {}
186impl<const ROUTE: u8> Select<Read> for KeyTag<ROUTE, Read> {}
187impl Select<Accept> for FrameworkTag<Accept> {}
188impl Select<Recv> for FrameworkTag<Recv> {}
189impl Select<Send> for FrameworkTag<Send> {}
190impl Select<Socket> for FrameworkTag<Socket> {}
191impl Select<Connect> for FrameworkTag<Connect> {}
192impl Select<kind::Shutdown> for FrameworkTag<kind::Shutdown> {}
193impl Select<Open> for FrameworkTag<Open> {}
194impl Select<kind::OneShot> for FrameworkTag<kind::OneShot> {}
195impl Select<kind::TaskQueue> for FrameworkTag<kind::TaskQueue> {}
196impl Select<Tuning> for FrameworkTag<Tuning> {}
197impl Select<Write> for FrameworkTag<Write> {}
198impl Select<Sync> for FrameworkTag<Sync> {}
199impl Select<Read> for FrameworkTag<Read> {}
200
201pub trait App: Tag + kind::Sealed {
203 const ID: Id;
204}
205
206pub trait Identity:
211 App<Class = kind::Any>
212 + Eq
213 + hash::Hash
214 + Materialize<Accept>
215 + Materialize<Recv>
216 + Materialize<Send>
217 + Materialize<Socket>
218 + Materialize<Connect>
219 + Materialize<kind::Shutdown>
220 + Materialize<Open>
221 + Materialize<kind::OneShot>
222 + Materialize<kind::TaskQueue>
223 + Materialize<Tuning>
224 + Materialize<Write>
225 + Materialize<Sync>
226 + Materialize<Read>
227{
228}
229
230pub trait Materialize<C: kind::Class>: Tag<Class = kind::Any> {
235 type With: App<Class = C> + Select<C>;
236}
237
238impl<const ROUTE: u8> Identity for KeyTag<ROUTE, kind::Any> {}
239
240impl<const ROUTE: u8, C: kind::Class> Materialize<C> for KeyTag<ROUTE, kind::Any>
241where
242 KeyTag<ROUTE, C>: App<Class = C> + Select<C>,
243{
244 type With = KeyTag<ROUTE, C>;
245}
246
247impl<const ROUTE: u8, C: kind::Class> PartialEq for KeyTag<ROUTE, C> {
250 fn eq(&self, _: &Self) -> bool {
251 true
252 }
253}
254
255impl<const ROUTE: u8, C: kind::Class> Eq for KeyTag<ROUTE, C> {}
256
257impl<const ROUTE: u8, C: kind::Class> hash::Hash for KeyTag<ROUTE, C> {
258 fn hash<H: hash::Hasher>(&self, state: &mut H) {
259 state.write_u8(ROUTE);
260 }
261}
262
263impl<const ROUTE: u8, C: kind::Class> Tag for KeyTag<ROUTE, C> {
264 const ROUTE: Route = Id::fixed::<ROUTE>().into_route();
265 type Class = C;
266}
267
268impl<const ROUTE: u8, C: kind::Class> App for KeyTag<ROUTE, C> {
269 const ID: Id = Id::fixed::<ROUTE>();
270}
271
272impl<const ROUTE: u8, C: kind::Class> kind::Sealed for KeyTag<ROUTE, C> {}
273
274impl<C: kind::Class> Tag for FrameworkTag<C> {
275 const ROUTE: Route = Route::FRAMEWORK;
276 type Class = C;
277}
278
279impl<T: Tag<Class = kind::Any>, C: kind::Class> Select<C> for T {}
280#[derive(Clone, Copy)]
282#[repr(transparent)]
283pub struct Operation<'d, T: Tag, C: kind::Class = <T as Tag>::Class> {
284 token: Token,
285 driver: Brand<'d, T>,
286 class: marker::PhantomData<C>,
287}
288
289impl<'d, T: Tag, C: kind::Class> Operation<'d, T, C> {
290 pub(crate) const fn new(token: Token, driver: Brand<'d, T>) -> Self {
291 Self {
292 token,
293 driver,
294 class: marker::PhantomData,
295 }
296 }
297
298 pub fn matches(self, target: Erased<'d, C>) -> bool {
299 self.token == target.token
300 }
301
302 pub const fn parts(self) -> table::Parts<T> {
303 table::Parts::new(self.token.slot(), self.token.epoch())
304 }
305
306 pub const fn erase_route(self) -> Erased<'d, C> {
307 Erased {
308 token: self.token,
309 flight: None,
310 driver: self.driver.driver,
311 class: marker::PhantomData,
312 }
313 }
314
315 pub(crate) const fn into_token(self) -> Token {
316 self.token
317 }
318}
319
320impl<T: Tag, C: kind::Class> PartialEq for Operation<'_, T, C> {
321 fn eq(&self, other: &Self) -> bool {
322 self.token == other.token
323 }
324}
325
326impl<T: Tag, C: kind::Class> Eq for Operation<'_, T, C> {}
327
328impl<T: Tag, C: kind::Class> hash::Hash for Operation<'_, T, C> {
329 fn hash<H: hash::Hasher>(&self, state: &mut H) {
330 self.token.hash(state);
331 }
332}
333
334pub struct Mismatch<'d, T: Tag, C: kind::Class> {
336 target: Erased<'d, C>,
337 tag: marker::PhantomData<T>,
338}
339
340impl<'d, T: Tag, C: kind::Class> Mismatch<'d, T, C> {
341 pub fn into_target(self) -> Erased<'d, C> {
342 self.target
343 }
344}
345
346pub mod kind;
347
348pub const SHUTDOWN: Control = Control::new(<kind::Shutdown as kind::Class>::CODE, SlotIndex::ZERO);
349
350const _: () = {
351 assert!(
352 mem::size_of::<Mismatch<'static, KeyTag<1>, Send>>()
353 == mem::size_of::<Erased<'static, Send>>()
354 );
355};
356const _: () = assert!(mem::size_of::<Id>() == mem::size_of::<u8>());
357const _: () = assert!(mem::size_of::<Route>() == mem::size_of::<u8>());
358const _: () = {
359 assert!(mem::size_of::<Operation<'static, KeyTag<1>>>() == mem::size_of::<Token>());
360 assert!(mem::align_of::<Operation<'static, KeyTag<1>>>() == mem::align_of::<Token>());
361};
362
363#[derive(Clone, Copy)]
365pub struct Space<'d, Tag: route::Tag> {
366 pub(in crate::driver) driver: route::Brand<'d, Tag>,
367}
368
369impl<'d, Tag: route::Tag> Space<'d, Tag> {
370 pub const fn bind(self, slot: indices::SlotIndex, epoch: Epoch) -> route::Target<'d, Tag> {
371 route::Target {
372 parts: table::Parts::new(slot, epoch),
373 driver: self.driver,
374 }
375 }
376
377 pub fn parse<C: kind::Class>(
378 self,
379 target: route::Erased<'d, C, impl Selection>,
380 ) -> Result<route::Operation<'d, Tag, C>, route::Mismatch<'d, Tag, C>>
381 where
382 Tag: route::Select<C>,
383 {
384 if target.route() != Tag::ROUTE {
385 return Err(Mismatch {
386 target: target.erase_route(),
387 tag: marker::PhantomData,
388 });
389 }
390 Ok(route::Operation::new(target.into_token(), self.driver))
391 }
392}
393
394const _: () = assert!(mem::size_of::<Space<'static, route::KeyTag<1>>>() == 0);
395
396#[derive(Clone, Copy, Debug)]
399#[repr(transparent)]
400pub struct Target<'d, Tag: route::Tag> {
401 parts: table::Parts<Tag>,
402 pub(in crate::driver) driver: route::Brand<'d, Tag>,
403}
404
405impl<'d, Tag: route::Tag> Target<'d, Tag> {
406 pub const fn parts(self) -> table::Parts<Tag> {
407 self.parts
408 }
409
410 pub const fn slot(self) -> indices::SlotIndex {
411 self.parts.slot()
412 }
413
414 pub const fn epoch(self) -> Epoch {
415 self.parts.epoch()
416 }
417
418 pub const fn bind<T>(self, value: T) -> route::Bound<'d, Tag, T> {
419 Bound {
420 target: self,
421 value,
422 }
423 }
424
425 pub const fn operation<C: kind::Class>(self) -> route::Operation<'d, Tag, C>
426 where
427 Tag: route::Select<C>,
428 {
429 let Self { parts, driver } = self;
430 route::Operation::new(route::Token::encode::<Tag, C>(parts), driver)
431 }
432
433 pub const fn dispatch(self) -> route::Operation<'d, Tag> {
434 let Self { parts, driver } = self;
435 route::Operation::new(route::Token::from_parts(parts), driver)
436 }
437}
438
439impl<'d, Tag: route::Tag, C: kind::Class> From<&route::Operation<'d, Tag, C>> for Target<'d, Tag> {
440 fn from(op: &route::Operation<'d, Tag, C>) -> Self {
441 Self {
442 parts: op.parts(),
443 driver: op.driver,
444 }
445 }
446}
447
448impl<Tag: route::Tag> PartialEq for Target<'_, Tag> {
449 fn eq(&self, other: &Self) -> bool {
450 self.parts == other.parts
451 }
452}
453
454impl<Tag: route::Tag> Eq for Target<'_, Tag> {}
455
456impl<Tag: route::Tag> hash::Hash for Target<'_, Tag> {
457 fn hash<H: hash::Hasher>(&self, state: &mut H) {
458 self.parts.hash(state);
459 }
460}
461
462const _: () = {
463 assert!(mem::size_of::<Target<'static, route::KeyTag<1>>>() == mem::size_of::<route::Token>());
464 assert!(
465 mem::align_of::<Target<'static, route::KeyTag<1>>>() == mem::align_of::<route::Token>()
466 );
467};
468
469#[must_use = "bound submission input must be submitted or dropped"]
472pub struct Bound<'d, Tag: route::Tag, T> {
473 target: route::Target<'d, Tag>,
474 value: T,
475}
476
477impl<'d, Tag: route::Tag, T> Bound<'d, Tag, T> {
478 pub(crate) fn into_parts(self) -> (route::Target<'d, Tag>, T) {
479 (self.target, self.value)
480 }
481}