use std::{convert::Infallible, marker::PhantomData, mem::MaybeUninit, os::fd::OwnedFd, ptr};
use o3::{
buffer::{bytes::bounded::Value, storage::provided},
mem::slot,
};
use crate::{
Errno,
io::reactor::complete::flags::Flow,
table::{closed, token::host::Flight},
};
pub mod fault;
pub mod kind;
pub mod raw;
type Received<'d> = Value<provided::raw::Slice<&'d mut [u8], provided::raw::Owner<'d, ()>>>;
pub type Accepted<F> =
Entry<F, kind::Accept, Item<Result<<F as Flight>::Descriptor, <F as Flight>::AcceptFailure>>>;
#[repr(C)]
pub struct Item<V> {
flow: Flow,
value: V,
}
impl<V> Item<V> {
pub const fn new(flow: Flow, value: V) -> Self {
Self { flow, value }
}
pub const fn flow(&self) -> Flow {
self.flow
}
pub const fn value(&self) -> &V {
&self.value
}
pub fn into_parts(self) -> (Flow, V) {
(self.flow, self.value)
}
}
#[repr(C)]
pub struct Entry<F: Flight, K: kind::Flight, V> {
target: F::Target<K>,
value: V,
flight: PhantomData<fn() -> F>,
}
impl<F: Flight, K: kind::Flight, V> Entry<F, K, V> {
pub const fn token(&self) -> F::Target<K> {
self.target
}
pub const fn value(&self) -> &V {
&self.value
}
pub fn into_parts(self) -> (F::Target<K>, V) {
(self.target, self.value)
}
}
impl<F: Flight, K: kind::Flight> Entry<F, K, ()> {
pub fn new(flight: F, proof: kind::Witness<K>, flow: Flow) -> Self {
Self {
target: flight.settle(proof, flow),
value: (),
flight: PhantomData,
}
}
pub fn with_value<V>(self, value: V) -> Entry<F, K, V> {
Entry {
target: self.target,
value,
flight: PhantomData,
}
}
}
impl<F: Flight, K: kind::Flight, V: Copy> Entry<F, K, V> {
pub const fn outcome(&self) -> V {
self.value
}
}
impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
pub const fn flow(&self) -> Flow {
self.value.flow
}
pub const fn data(&self) -> &V {
&self.value.value
}
pub fn into_batch(self) -> (F::Target<K>, Flow, V) {
(self.target, self.value.flow, self.value.value)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Connect {
Option(Errno),
Connect(Errno),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct Retryable(Errno);
impl From<Retryable> for Errno {
fn from(error: Retryable) -> Self {
error.0
}
}
#[repr(u8)]
pub enum Kind<'d, F: Flight> {
Accept(Accepted<F>),
RecvData(Entry<F, kind::Recv, Item<Received<'d>>>) = 1,
RecvEof(Entry<F, kind::Recv, Item<()>>),
RecvCancelled(Entry<F, kind::Recv, Item<()>>),
RecvExhausted(Entry<F, kind::Recv, Item<F::Exhausted>>),
RecvStarved(Entry<F, kind::Recv, Item<Retryable>>),
RecvFailed(Entry<F, kind::Recv, Item<Errno>>),
Send(Entry<F, kind::Send, Result<F::Length, Errno>>),
SendOwned(F::Owned),
Socket(Entry<F, kind::Socket, Result<F::Created, F::SocketFailure>>),
Tuning(Entry<F, kind::Tuning, Result<u32, Errno>>),
Connect(Entry<F, kind::Connect, Result<(), Connect>>),
Open(Entry<F, kind::Open, Result<OwnedFd, Errno>>),
Write(Entry<F, kind::Write, Result<F::Length, Errno>>),
Read(Entry<F, kind::Read, Result<F::Length, Errno>>),
Sync(Entry<F, kind::Sync, Result<(), Errno>>),
}
impl<F: Flight, K: kind::Flight, V> Entry<F, K, V> {
fn project<'event>(
owner: slot::Occupied<'event, Self>,
) -> (F::Target<K>, slot::Occupied<'event, V>) {
let target = owner.target;
let entry = owner.into_uninit().get_mut().as_mut_ptr();
let value = unsafe {
slot::raw::Vacant::new(&mut *(&raw mut (*entry).value).cast::<MaybeUninit<V>>())
.assume_init()
};
(target, value)
}
}
impl<F: Flight, K: kind::Flight, V> Entry<F, K, Item<V>> {
pub fn consume<'event>(
owner: slot::Occupied<'event, Self>,
) -> (F::Target<K>, Flow, slot::Occupied<'event, V>) {
let target = owner.target;
let flow = owner.value.flow;
let entry = owner.into_uninit().get_mut().as_mut_ptr();
let value = unsafe {
slot::raw::Vacant::new(&mut *(&raw mut (*entry).value.value).cast::<MaybeUninit<V>>())
.assume_init()
};
(target, flow, value)
}
}
impl<F: Flight, K: kind::Flight, V, E: Copy> Entry<F, K, Result<V, E>> {
pub fn consume<'event>(
owner: slot::Occupied<'event, Self>,
) -> (F::Target<K>, Result<slot::Occupied<'event, V>, E>) {
let (target, value) = Self::project(owner);
(target, project(value))
}
}
impl<F: Flight, V, E: Copy> Entry<F, kind::Accept, Item<Result<V, E>>> {
pub fn accepted<'event>(
owner: slot::Occupied<'event, Self>,
) -> (
F::Target<kind::Accept>,
Flow,
Result<slot::Occupied<'event, V>, E>,
) {
let (target, flow, value) = Self::consume(owner);
(target, flow, project(value))
}
}
fn project<'event, V, E: Copy>(
owner: slot::Occupied<'event, Result<V, E>>,
) -> Result<slot::Occupied<'event, V>, E> {
let result = owner.into_uninit().get_mut().as_mut_ptr();
let value = match unsafe { &mut *result } {
Ok(value) => ptr::from_mut(value),
Err(error) => return Err(*error),
};
Ok(unsafe { slot::raw::Vacant::new(&mut *value.cast::<MaybeUninit<V>>()).assume_init() })
}
pub trait Report {
type Event;
fn report(&mut self, event: Self::Event);
}
#[derive(Debug)]
pub enum Reclaimed<F, P, C, T, S, A, E, L, M, R, X> {
Fault(F),
Reactor(P),
Cancellation(C),
Close {
slot: S,
file: closed::File<M>,
reactor: Result<(), P>,
},
Accept {
echo: E,
result: Result<R, A>,
flow: Flow,
},
AcceptRoute {
target: T,
cause: A,
flow: Flow,
},
Recv {
echo: E,
result: Result<usize, Errno>,
flow: Flow,
},
RecvRoute {
target: T,
result: Result<L, Errno>,
flow: Flow,
},
Send {
echo: E,
result: Result<L, Errno>,
},
Connect {
echo: E,
result: Result<(), Connect>,
},
Socket {
echo: E,
slot: S,
result: Result<(), X>,
close: Option<Result<(), Errno>>,
},
Open {
echo: E,
result: Result<(), Errno>,
close: Option<Result<(), Errno>>,
},
Write {
echo: E,
result: Result<L, Errno>,
},
Read {
echo: E,
result: Result<L, Errno>,
},
Sync {
echo: E,
result: Result<(), Errno>,
},
Tuning {
echo: E,
result: Result<(), Errno>,
},
Shutdown,
}
impl<F, P, C: Copy, T, S, A, E, L, M, R, X> From<&C>
for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
{
fn from(failure: &C) -> Self {
Self::Cancellation(*failure)
}
}
impl<F, P, C, T, S, A, E, L, M, R, X, S0, M0, P0> From<closed::Completed<S0, M0, P0>>
for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
where
S: From<S0>,
M: From<M0>,
P: From<P0>,
{
fn from(done: closed::Completed<S0, M0, P0>) -> Self {
Self::Close {
slot: done.slot.into(),
file: match done.file {
closed::File::Missing(cause) => closed::File::Missing(cause.into()),
closed::File::Closed(result) => closed::File::Closed(result),
},
reactor: match done.reactor {
Ok(()) => Ok(()),
Err(cause) => Err(cause.into()),
},
}
}
}
impl<F, P, C, T, S, A, E, L, M, R, X> From<Infallible>
for Reclaimed<F, P, C, T, S, A, E, L, M, R, X>
{
fn from(value: Infallible) -> Self {
match value {}
}
}