use std::{ops::ControlFlow, task};
use o3::{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 type Progress<H> = (ControlFlow<H, Drain>, ControlFlow<()>);
pub type Dispatch<H, P, F> = Result<Progress<H>, Failure<P, F>>;
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 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>),
}
impl<P, F> From<(task::Poll<()>, F)> for Failure<P, F> {
fn from((state, fault): (task::Poll<()>, F)) -> Self {
Self::Completion(fault::Dispatch {
drain: state.into(),
fault,
})
}
}
#[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,
}
}
}