Skip to main content

dope_sys/io/reactor/event/
mod.rs

1use std::{convert::Infallible, marker::PhantomData, os::fd::OwnedFd};
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, ()>>>;
19type Accepted<F, V, E> = Entry<F, kind::Accept, Item<Result<V, E>>>;
20
21#[repr(C)]
22pub struct Item<V> {
23    flow: Flow,
24    value: V,
25}
26
27impl<V> Item<V> {
28    pub const fn new(flow: Flow, value: V) -> Self {
29        Self { flow, value }
30    }
31    pub const fn flow(&self) -> Flow {
32        self.flow
33    }
34    pub const fn value(&self) -> &V {
35        &self.value
36    }
37    pub fn into_parts(self) -> (Flow, V) {
38        (self.flow, self.value)
39    }
40}
41
42#[repr(C)]
43pub struct Entry<F: Flight, K: kind::Flight, V> {
44    target: F::Target<K>,
45    value: V,
46    flight: PhantomData<fn() -> F>,
47}
48
49impl<F: Flight, K: kind::Flight, V> Entry<F, K, V> {
50    pub const fn token(&self) -> F::Target<K> {
51        self.target
52    }
53    pub const fn value(&self) -> &V {
54        &self.value
55    }
56    pub fn into_parts(self) -> (F::Target<K>, V) {
57        (self.target, self.value)
58    }
59}
60
61impl<F: Flight, K: kind::Flight> Entry<F, K, ()> {
62    pub fn new(flight: F, proof: kind::Witness<K>, flow: Flow) -> Self {
63        Self {
64            target: flight.settle(proof, flow),
65            value: (),
66            flight: PhantomData,
67        }
68    }
69
70    pub fn with_value<V>(self, value: V) -> Entry<F, K, V> {
71        Entry {
72            target: self.target,
73            value,
74            flight: PhantomData,
75        }
76    }
77}
78
79impl<F: Flight, K: kind::Flight, V: Copy> Entry<F, K, V> {
80    pub const fn outcome(&self) -> V {
81        self.value
82    }
83}
84
85impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
86    pub const fn flow(&self) -> Flow {
87        self.value.flow
88    }
89    pub const fn data(&self) -> &V {
90        &self.value.value
91    }
92
93    pub fn into_batch(self) -> (F::Target<K>, Flow, V) {
94        (self.target, self.value.flow, self.value.value)
95    }
96}
97
98#[derive(Clone, Copy, Debug, Eq, PartialEq)]
99pub enum Connect {
100    Option(Errno),
101    Connect(Errno),
102}
103
104#[derive(Clone, Copy, Debug, Eq, PartialEq)]
105#[repr(transparent)]
106pub struct Retryable(Errno);
107
108impl From<Retryable> for Errno {
109    fn from(error: Retryable) -> Self {
110        error.0
111    }
112}
113
114#[repr(u8)]
115pub enum Kind<'d, F: Flight> {
116    Accept(Accepted<F, F::Descriptor, F::AcceptFailure>),
117    RecvData(Entry<F, kind::Recv, Item<Received<'d>>>) = 1,
118    RecvEof(Entry<F, kind::Recv, Item<()>>),
119    RecvCancelled(Entry<F, kind::Recv, Item<()>>),
120    RecvExhausted(Entry<F, kind::Recv, Item<F::Exhausted>>),
121    RecvStarved(Entry<F, kind::Recv, Item<Retryable>>),
122    RecvFailed(Entry<F, kind::Recv, Item<Errno>>),
123    Send(Entry<F, kind::Send, Result<F::Length, Errno>>),
124    SendOwned(F::Owned),
125    Socket(Entry<F, kind::Socket, Result<F::Created, F::SocketFailure>>),
126    Tuning(Entry<F, kind::Tuning, Result<u32, Errno>>),
127    Connect(Entry<F, kind::Connect, Result<(), Connect>>),
128    Open(Entry<F, kind::Open, Result<OwnedFd, Errno>>),
129    Write(Entry<F, kind::Write, Result<F::Length, Errno>>),
130    Read(Entry<F, kind::Read, Result<F::Length, Errno>>),
131    Sync(Entry<F, kind::Sync, Result<(), Errno>>),
132}
133
134impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
135    pub fn consume<'event>(
136        owner: slot::Occupied<'event, Self>,
137    ) -> (F::Target<K>, Flow, slot::Occupied<'event, V>) {
138        raw::batch(owner)
139    }
140}
141
142impl<F: Flight, K: kind::Flight, V, E: Copy> Entry<F, K, Result<V, E>> {
143    pub fn consume<'event>(
144        owner: slot::Occupied<'event, Self>,
145    ) -> (F::Target<K>, Result<slot::Occupied<'event, V>, E>) {
146        raw::outcome(owner)
147    }
148}
149
150impl<F: Flight, V, E: Copy> Accepted<F, V, E> {
151    pub fn accepted<'event>(
152        owner: slot::Occupied<'event, Self>,
153    ) -> (
154        F::Target<kind::Accept>,
155        Flow,
156        Result<slot::Occupied<'event, V>, E>,
157    ) {
158        raw::accepted(owner)
159    }
160}
161
162pub trait Report {
163    type Event;
164    fn report(&mut self, event: Self::Event);
165}
166
167#[derive(Debug)]
168pub enum Reclaimed<F, P, C, T, S, A, E, L, M, R, X> {
169    Fault(F),
170    Reactor(P),
171    Cancellation(C),
172    Close {
173        slot: S,
174        file: closed::File<M>,
175        reactor: Result<(), P>,
176    },
177    Accept {
178        echo: E,
179        result: Result<R, A>,
180        flow: Flow,
181    },
182    AcceptRoute {
183        target: T,
184        cause: A,
185        flow: Flow,
186    },
187    Recv {
188        echo: E,
189        result: Result<usize, Errno>,
190        flow: Flow,
191    },
192    RecvRoute {
193        target: T,
194        result: Result<L, Errno>,
195        flow: Flow,
196    },
197    Send {
198        echo: E,
199        result: Result<L, Errno>,
200    },
201    Connect {
202        echo: E,
203        result: Result<(), Connect>,
204    },
205    Socket {
206        echo: E,
207        slot: S,
208        result: Result<(), X>,
209        close: Option<Result<(), Errno>>,
210    },
211    Open {
212        echo: E,
213        result: Result<(), Errno>,
214        close: Option<Result<(), Errno>>,
215    },
216    Write {
217        echo: E,
218        result: Result<L, Errno>,
219    },
220    Read {
221        echo: E,
222        result: Result<L, Errno>,
223    },
224    Sync {
225        echo: E,
226        result: Result<(), Errno>,
227    },
228    Tuning {
229        echo: E,
230        result: Result<(), Errno>,
231    },
232    Shutdown,
233}
234
235impl<F, P, C: Copy, T, S, A, E, L, M, R, X> From<&C>
236    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
237{
238    fn from(failure: &C) -> Self {
239        Self::Cancellation(*failure)
240    }
241}
242
243impl<F, P, C, T, S, A, E, L, M, R, X, S0, M0, P0> From<closed::Completed<S0, M0, P0>>
244    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
245where
246    S: From<S0>,
247    M: From<M0>,
248    P: From<P0>,
249{
250    fn from(done: closed::Completed<S0, M0, P0>) -> Self {
251        Self::Close {
252            slot: done.slot.into(),
253            file: match done.file {
254                closed::File::Missing(cause) => closed::File::Missing(cause.into()),
255                closed::File::Closed(result) => closed::File::Closed(result),
256            },
257            reactor: match done.reactor {
258                Ok(()) => Ok(()),
259                Err(cause) => Err(cause.into()),
260            },
261        }
262    }
263}
264
265impl<F, P, C, T, S, A, E, L, M, R, X> From<Infallible>
266    for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
267{
268    fn from(value: Infallible) -> Self {
269        match value {}
270    }
271}