use std::{ops::ControlFlow, task};
use o3::{alloc, buffer, mem::slot};
use crate::{io, io::reactor::event::fault, memory};
pub mod admit;
mod cancel;
mod install;
pub mod raw;
pub mod register;
pub mod ring;
pub mod shutdown;
pub use cancel::Cancel;
pub use install::Install;
pub trait Buffer {
type Layout: memory::raw::Layout;
type Token;
type Context: memory::Context<Layout = Self::Layout> + buffer::raw::Fixed<Self::Token>;
type Retained;
type Arena<C>;
type Root<'d, C>: Copy
where
C: 'd,
Self::Retained: 'd;
}
pub trait Source<C> {
type Backend: io::Close<State = Self::Closing> + shutdown::Shutdown;
type Closing: io::Retry;
}
pub trait Storage<C, L, P: Copy, R: Copy>: Removal {
type Cleanup: memory::Cleanup<P>;
type Release: memory::Cleanup<(R, Self::Slot)>;
}
pub enum Rejected<F, C, R> {
Refused(F),
Cleanup(C),
Release(R),
}
impl<F: From<alloc::Error>, C, R> From<alloc::Error> for Rejected<F, C, R> {
fn from(cause: alloc::Error) -> Self {
Self::Refused(cause.into())
}
}
impl<P: Into<F>, F, C, R> From<Result<P, C>> for Rejected<F, C, R> {
fn from(result: Result<P, C>) -> Self {
match result {
Ok(cause) => Self::Refused(cause.into()),
Err(state) => Self::Cleanup(state),
}
}
}
pub trait Removal {
type Slot: Copy + Eq;
}
pub trait Pending {
fn has_deferred(&self) -> bool;
}
pub trait Mode<'d, Q = (), O = ()>: Sized {
type Event;
type Held;
type Work: Copy;
type Pending<'p>
where
'd: 'p;
fn replay<'p>(
&mut self,
work: Self::Work,
pending: &mut Self::Pending<'p>,
) -> ControlFlow<Self::Held>
where
'd: 'p;
fn emit<'p>(
&mut self,
event: slot::Occupied<'_, Self::Event>,
work: Self::Work,
pending: &mut Self::Pending<'p>,
) -> ControlFlow<Self::Held>
where
'd: 'p;
fn park(&mut self);
fn observed(observer: &O) -> ControlFlow<()>;
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Failure<P, F> {
Native(P),
Completion(fault::Dispatch<F>),
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[must_use = "pending backend events must be drained before blocking"]
pub enum Drain {
Drained,
Pending,
}
impl From<task::Poll<()>> for Drain {
fn from(state: task::Poll<()>) -> Self {
match state {
task::Poll::Ready(()) => Self::Drained,
task::Poll::Pending => Self::Pending,
}
}
}