Skip to main content

dope_sys/io/reactor/event/
mod.rs

1use std::{convert::Infallible, marker::PhantomData, mem::MaybeUninit, os::fd::OwnedFd, ptr};
2
3use o3::{
4    buffer::{bytes::bounded::Value, storage::provided},
5    mem::slot,
6};
7
8use crate::{
9    Errno,
10    io::reactor::complete::flags::Flow,
11    table::{closed, token::host::Flight},
12};
13
14pub mod fault;
15pub mod kind;
16pub mod raw;
17
18type Received<'d> = Value<provided::raw::Slice<&'d mut [u8], provided::raw::Owner<'d, ()>>>;
19pub type Accepted<F> =
20    Entry<F, kind::Accept, Item<Result<<F as Flight>::Descriptor, <F as Flight>::AcceptFailure>>>;
21
22#[repr(C)]
23pub struct Item<V> {
24    flow: Flow,
25    value: V,
26}
27
28impl<V> Item<V> {
29    pub const fn new(flow: Flow, value: V) -> Self {
30        Self { flow, value }
31    }
32    pub const fn flow(&self) -> Flow {
33        self.flow
34    }
35    pub const fn value(&self) -> &V {
36        &self.value
37    }
38    pub fn into_parts(self) -> (Flow, V) {
39        (self.flow, self.value)
40    }
41}
42
43#[repr(C)]
44pub struct Entry<F: Flight, K: kind::Flight, V> {
45    target: F::Target<K>,
46    value: V,
47    flight: PhantomData<fn() -> F>,
48}
49
50impl<F: Flight, K: kind::Flight, V> Entry<F, K, V> {
51    pub const fn token(&self) -> F::Target<K> {
52        self.target
53    }
54    pub const fn value(&self) -> &V {
55        &self.value
56    }
57    pub fn into_parts(self) -> (F::Target<K>, V) {
58        (self.target, self.value)
59    }
60}
61
62impl<F: Flight, K: kind::Flight> Entry<F, K, ()> {
63    pub fn new(flight: F, proof: kind::Witness<K>, flow: Flow) -> Self {
64        Self {
65            target: flight.settle(proof, flow),
66            value: (),
67            flight: PhantomData,
68        }
69    }
70
71    pub fn with_value<V>(self, value: V) -> Entry<F, K, V> {
72        Entry {
73            target: self.target,
74            value,
75            flight: PhantomData,
76        }
77    }
78}
79
80impl<F: Flight, K: kind::Flight, V: Copy> Entry<F, K, V> {
81    pub const fn outcome(&self) -> V {
82        self.value
83    }
84}
85
86impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
87    pub const fn flow(&self) -> Flow {
88        self.value.flow
89    }
90    pub const fn data(&self) -> &V {
91        &self.value.value
92    }
93
94    pub fn into_batch(self) -> (F::Target<K>, Flow, V) {
95        (self.target, self.value.flow, self.value.value)
96    }
97}
98
99#[derive(Clone, Copy, Debug, Eq, PartialEq)]
100pub enum Connect {
101    Option(Errno),
102    Connect(Errno),
103}
104
105#[derive(Clone, Copy, Debug, Eq, PartialEq)]
106#[repr(transparent)]
107pub struct Retryable(Errno);
108
109impl From<Retryable> for Errno {
110    fn from(error: Retryable) -> Self {
111        error.0
112    }
113}
114
115#[repr(u8)]
116pub enum Kind<'d, F: Flight> {
117    Accept(Accepted<F>),
118    RecvData(Entry<F, kind::Recv, Item<Received<'d>>>) = 1,
119    RecvEof(Entry<F, kind::Recv, Item<()>>),
120    RecvCancelled(Entry<F, kind::Recv, Item<()>>),
121    RecvExhausted(Entry<F, kind::Recv, Item<F::Exhausted>>),
122    RecvStarved(Entry<F, kind::Recv, Item<Retryable>>),
123    RecvFailed(Entry<F, kind::Recv, Item<Errno>>),
124    Send(Entry<F, kind::Send, Result<F::Length, Errno>>),
125    SendOwned(F::Owned),
126    Socket(Entry<F, kind::Socket, Result<F::Created, F::SocketFailure>>),
127    Tuning(Entry<F, kind::Tuning, Result<u32, Errno>>),
128    Connect(Entry<F, kind::Connect, Result<(), Connect>>),
129    Open(Entry<F, kind::Open, Result<OwnedFd, Errno>>),
130    Write(Entry<F, kind::Write, Result<F::Length, Errno>>),
131    Read(Entry<F, kind::Read, Result<F::Length, Errno>>),
132    Sync(Entry<F, kind::Sync, Result<(), Errno>>),
133}
134
135impl<F: Flight, K: kind::Flight, V> Entry<F, K, V> {
136    fn project<'event>(
137        owner: slot::Occupied<'event, Self>,
138    ) -> (F::Target<K>, slot::Occupied<'event, V>) {
139        let target = owner.target;
140        let entry = owner.into_uninit().get_mut().as_mut_ptr();
141        // SAFETY: Entry's copied target has no destructor; this transfers its sole value.
142        let value = unsafe {
143            slot::raw::Vacant::new(&mut *(&raw mut (*entry).value).cast::<MaybeUninit<V>>())
144                .assume_init()
145        };
146        (target, value)
147    }
148}
149
150impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
151    pub fn consume<'event>(
152        owner: slot::Occupied<'event, Self>,
153    ) -> (F::Target<K>, Flow, slot::Occupied<'event, V>) {
154        let target = owner.target;
155        let flow = owner.value.flow;
156        let entry = owner.into_uninit().get_mut().as_mut_ptr();
157        // SAFETY: target and Flow are copied headers; only this field owns destruction.
158        let value = unsafe {
159            slot::raw::Vacant::new(&mut *(&raw mut (*entry).value.value).cast::<MaybeUninit<V>>())
160                .assume_init()
161        };
162        (target, flow, value)
163    }
164}
165
166impl<F: Flight, K: kind::Flight, V, E: Copy> Entry<F, K, Result<V, E>> {
167    pub fn consume<'event>(
168        owner: slot::Occupied<'event, Self>,
169    ) -> (F::Target<K>, Result<slot::Occupied<'event, V>, E>) {
170        let (target, value) = Self::project(owner);
171        (target, project(value))
172    }
173}
174
175impl<F: Flight, V, E: Copy> Entry<F, kind::Accept, Item<Result<V, E>>> {
176    pub fn accepted<'event>(
177        owner: slot::Occupied<'event, Self>,
178    ) -> (
179        F::Target<kind::Accept>,
180        Flow,
181        Result<slot::Occupied<'event, V>, E>,
182    ) {
183        let (target, flow, value) = Self::consume(owner);
184        (target, flow, project(value))
185    }
186}
187
188fn project<'event, V, E: Copy>(
189    owner: slot::Occupied<'event, Result<V, E>>,
190) -> Result<slot::Occupied<'event, V>, E> {
191    let result = owner.into_uninit().get_mut().as_mut_ptr();
192    // SAFETY: the consumed Occupied transferred its initialized result unchanged.
193    let value = match unsafe { &mut *result } {
194        Ok(value) => ptr::from_mut(value),
195        Err(error) => return Err(*error),
196    };
197    // SAFETY: the selected success field retains the result owner's only destructor.
198    Ok(unsafe { slot::raw::Vacant::new(&mut *value.cast::<MaybeUninit<V>>()).assume_init() })
199}
200
201pub trait Report {
202    type Event;
203    fn report(&mut self, event: Self::Event);
204}
205
206#[derive(Debug)]
207pub enum Reclaimed<F, P, C, T, S, A, E, L, M, R, X> {
208    Fault(F),
209    Reactor(P),
210    Cancellation(C),
211    Close {
212        slot: S,
213        file: closed::File<M>,
214        reactor: Result<(), P>,
215    },
216    Accept {
217        echo: E,
218        result: Result<R, A>,
219        flow: Flow,
220    },
221    AcceptRoute {
222        target: T,
223        cause: A,
224        flow: Flow,
225    },
226    Recv {
227        echo: E,
228        result: Result<usize, Errno>,
229        flow: Flow,
230    },
231    RecvRoute {
232        target: T,
233        result: Result<L, Errno>,
234        flow: Flow,
235    },
236    Send {
237        echo: E,
238        result: Result<L, Errno>,
239    },
240    Connect {
241        echo: E,
242        result: Result<(), Connect>,
243    },
244    Socket {
245        echo: E,
246        slot: S,
247        result: Result<(), X>,
248        close: Option<Result<(), Errno>>,
249    },
250    Open {
251        echo: E,
252        result: Result<(), Errno>,
253        close: Option<Result<(), Errno>>,
254    },
255    Write {
256        echo: E,
257        result: Result<L, Errno>,
258    },
259    Read {
260        echo: E,
261        result: Result<L, Errno>,
262    },
263    Sync {
264        echo: E,
265        result: Result<(), Errno>,
266    },
267    Tuning {
268        echo: E,
269        result: Result<(), Errno>,
270    },
271    Shutdown,
272}
273
274impl<F, P, C: Copy, T, S, A, E, L, M, R, X> From<&C>
275    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
276{
277    fn from(failure: &C) -> Self {
278        Self::Cancellation(*failure)
279    }
280}
281
282impl<F, P, C, T, S, A, E, L, M, R, X, S0, M0, P0> From<closed::Completed<S0, M0, P0>>
283    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
284where
285    S: From<S0>,
286    M: From<M0>,
287    P: From<P0>,
288{
289    fn from(done: closed::Completed<S0, M0, P0>) -> Self {
290        Self::Close {
291            slot: done.slot.into(),
292            file: match done.file {
293                closed::File::Missing(cause) => closed::File::Missing(cause.into()),
294                closed::File::Closed(result) => closed::File::Closed(result),
295            },
296            reactor: match done.reactor {
297                Ok(()) => Ok(()),
298                Err(cause) => Err(cause.into()),
299            },
300        }
301    }
302}
303
304impl<F, P, C, T, S, A, E, L, M, R, X> From<Infallible>
305    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
306{
307    fn from(value: Infallible) -> Self {
308        match value {}
309    }
310}