Skip to main content

dope_core/driver/route/kind/
mod.rs

1use core::{marker, mem};
2
3use crate::driver::route::{FrameworkTag, KeyTag, Select};
4
5mod sealed;
6pub(super) use sealed::Sealed;
7
8/// Compile-time proof of one operation class accepted by the driver.
9pub trait Class: Sealed + Copy {
10    /// Operation class this marker selects.
11    const KIND: Kind;
12
13    /// Protocol code of this class, derived from its single class tag.
14    const CODE: Code = Code::new(Self::KIND as u8);
15}
16
17/// An operation class which can own one native flight.
18///
19/// An unclassified target cannot enter the flight arena:
20///
21/// Framework controls do not own native flights:
22pub trait Flight: Class {
23    /// Native flight class of this operation class.
24    const FLIGHT: Native;
25}
26
27/// The closed set of operation classes stored in the native flight arena.
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29#[repr(u8)]
30pub enum Native {
31    Accept = 1,
32    Recv = 2,
33    Send = 3,
34    Socket = 5,
35    Connect = 6,
36    Open = 11,
37    Tuning = 25,
38    Write = 28,
39    Sync = 29,
40}
41
42/// Opaque operation-class code used only by the route encoder.
43#[derive(Clone, Copy, Eq, PartialEq)]
44#[repr(transparent)]
45pub struct Code(u8);
46
47impl Code {
48    pub(crate) const fn new(raw: u8) -> Self {
49        Self(raw)
50    }
51
52    pub(crate) const fn raw(self) -> u8 {
53        self.0
54    }
55}
56
57const _: () = assert!(mem::size_of::<Code>() == mem::size_of::<u8>());
58
59/// Zero-sized proof produced only by decoding one observed operation class.
60#[derive(Clone, Copy)]
61pub struct Witness<C: Class>(pub(super) marker::PhantomData<C>);
62
63impl<C: Class> Witness<C> {
64    const NEW: Self = Self(marker::PhantomData);
65
66    pub(crate) const fn marker(self) -> marker::PhantomData<C> {
67        self.0
68    }
69}
70
71#[derive(Clone, Copy)]
72#[repr(u8)]
73pub(crate) enum Shape {
74    Socket(Witness<Socket>),
75    Open(Witness<Open>),
76    Accept(Witness<Accept>),
77    Recv(Witness<Recv>),
78    Send(Witness<Send>),
79    Write(Witness<Write>),
80    Connect(Witness<Connect>),
81    Sync(Witness<Sync>),
82    Tuning(Witness<Tuning>),
83}
84
85#[derive(Clone, Copy, Eq, PartialEq, Debug)]
86#[repr(u8)]
87pub enum Kind {
88    Unspecified = 0,
89    Accept = 1,
90    Recv = 2,
91    Send = 3,
92    Socket = 5,
93    Connect = 6,
94    Shutdown = 7,
95    Open = 11,
96    OneShot = 19,
97    TaskQueue = 21,
98    Tuning = 25,
99    Write = 28,
100    Sync = 29,
101}
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104#[repr(transparent)]
105pub struct Unknown(u8);
106
107#[derive(Clone, Copy, Debug)]
108pub enum Any {}
109#[derive(Clone, Copy)]
110pub enum Accept {}
111#[derive(Clone, Copy)]
112pub enum Recv {}
113#[derive(Clone, Copy)]
114pub enum Send {}
115#[derive(Clone, Copy)]
116pub enum Socket {}
117#[derive(Clone, Copy)]
118pub enum Connect {}
119#[derive(Clone, Copy)]
120pub enum Shutdown {}
121#[derive(Clone, Copy)]
122pub enum Open {}
123#[derive(Clone, Copy)]
124pub enum OneShot {}
125#[derive(Clone, Copy)]
126pub enum TaskQueue {}
127#[derive(Clone, Copy)]
128pub enum Tuning {}
129#[derive(Clone, Copy)]
130pub enum Write {}
131#[derive(Clone, Copy)]
132pub enum Sync {}
133impl Sealed for Any {}
134impl Sealed for Accept {}
135impl Sealed for Recv {}
136impl Sealed for Send {}
137impl Sealed for Socket {}
138impl Sealed for Connect {}
139impl Sealed for Shutdown {}
140impl Sealed for Open {}
141impl Sealed for OneShot {}
142impl Sealed for TaskQueue {}
143impl Sealed for Tuning {}
144impl Sealed for Write {}
145impl Sealed for Sync {}
146impl Class for Any {
147    const KIND: Kind = Kind::Unspecified;
148}
149
150impl Class for Accept {
151    const KIND: Kind = Kind::Accept;
152}
153
154impl Class for Recv {
155    const KIND: Kind = Kind::Recv;
156}
157
158impl Class for Send {
159    const KIND: Kind = Kind::Send;
160}
161
162impl Class for Socket {
163    const KIND: Kind = Kind::Socket;
164}
165
166impl Class for Connect {
167    const KIND: Kind = Kind::Connect;
168}
169
170impl Class for Shutdown {
171    const KIND: Kind = Kind::Shutdown;
172}
173
174impl Class for Open {
175    const KIND: Kind = Kind::Open;
176}
177
178impl Class for OneShot {
179    const KIND: Kind = Kind::OneShot;
180}
181
182impl Class for TaskQueue {
183    const KIND: Kind = Kind::TaskQueue;
184}
185
186impl Class for Tuning {
187    const KIND: Kind = Kind::Tuning;
188}
189
190impl Class for Write {
191    const KIND: Kind = Kind::Write;
192}
193
194impl Class for Sync {
195    const KIND: Kind = Kind::Sync;
196}
197
198impl<const ROUTE: u8> Select<Accept> for KeyTag<ROUTE, Accept> {}
199impl<const ROUTE: u8> Select<Recv> for KeyTag<ROUTE, Recv> {}
200impl<const ROUTE: u8> Select<Send> for KeyTag<ROUTE, Send> {}
201impl<const ROUTE: u8> Select<Socket> for KeyTag<ROUTE, Socket> {}
202impl<const ROUTE: u8> Select<Connect> for KeyTag<ROUTE, Connect> {}
203impl<const ROUTE: u8> Select<Shutdown> for KeyTag<ROUTE, Shutdown> {}
204impl<const ROUTE: u8> Select<Open> for KeyTag<ROUTE, Open> {}
205impl<const ROUTE: u8> Select<OneShot> for KeyTag<ROUTE, OneShot> {}
206impl<const ROUTE: u8> Select<TaskQueue> for KeyTag<ROUTE, TaskQueue> {}
207impl<const ROUTE: u8> Select<Tuning> for KeyTag<ROUTE, Tuning> {}
208impl<const ROUTE: u8> Select<Write> for KeyTag<ROUTE, Write> {}
209impl<const ROUTE: u8> Select<Sync> for KeyTag<ROUTE, Sync> {}
210impl Select<Accept> for FrameworkTag<Accept> {}
211impl Select<Recv> for FrameworkTag<Recv> {}
212impl Select<Send> for FrameworkTag<Send> {}
213impl Select<Socket> for FrameworkTag<Socket> {}
214impl Select<Connect> for FrameworkTag<Connect> {}
215impl Select<Shutdown> for FrameworkTag<Shutdown> {}
216impl Select<Open> for FrameworkTag<Open> {}
217impl Select<OneShot> for FrameworkTag<OneShot> {}
218impl Select<TaskQueue> for FrameworkTag<TaskQueue> {}
219impl Select<Tuning> for FrameworkTag<Tuning> {}
220impl Select<Write> for FrameworkTag<Write> {}
221impl Select<Sync> for FrameworkTag<Sync> {}
222
223impl Flight for Accept {
224    const FLIGHT: Native = Native::Accept;
225}
226
227impl Flight for Recv {
228    const FLIGHT: Native = Native::Recv;
229}
230
231impl Flight for Send {
232    const FLIGHT: Native = Native::Send;
233}
234
235impl Flight for Socket {
236    const FLIGHT: Native = Native::Socket;
237}
238
239impl Flight for Connect {
240    const FLIGHT: Native = Native::Connect;
241}
242
243impl Flight for Open {
244    const FLIGHT: Native = Native::Open;
245}
246
247impl Flight for Tuning {
248    const FLIGHT: Native = Native::Tuning;
249}
250
251impl Flight for Write {
252    const FLIGHT: Native = Native::Write;
253}
254
255impl Flight for Sync {
256    const FLIGHT: Native = Native::Sync;
257}
258
259impl Native {
260    pub(crate) fn witness<C: Flight>(self) -> Option<Witness<C>> {
261        (self == C::FLIGHT).then_some(Witness::NEW)
262    }
263}
264
265impl From<Native> for Shape {
266    fn from(native: Native) -> Self {
267        match native {
268            Native::Socket => Self::Socket(Witness::NEW),
269            Native::Open => Self::Open(Witness::NEW),
270            Native::Accept => Self::Accept(Witness::NEW),
271            Native::Recv => Self::Recv(Witness::NEW),
272            Native::Send => Self::Send(Witness::NEW),
273            Native::Write => Self::Write(Witness::NEW),
274            Native::Connect => Self::Connect(Witness::NEW),
275            Native::Sync => Self::Sync(Witness::NEW),
276            Native::Tuning => Self::Tuning(Witness::NEW),
277        }
278    }
279}
280
281impl Unknown {
282    pub(crate) const fn new(raw: u8) -> Self {
283        Self(raw)
284    }
285}
286
287const _: () = {
288    assert!(mem::size_of::<Shape>() == 1);
289    assert!(mem::size_of::<Native>() == mem::size_of::<Kind>());
290};