dope-sys 0.76.0

Typed operating-system call contracts
Documentation
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>>;

/// Exact completed receive ownership and its original projection context.
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;
}

/// Selects the native backend owner for its host's typed contracts.
pub trait Source<C> {
    type Backend: io::Close<State = Self::Closing> + shutdown::Shutdown;
    type Closing: io::Retry;
}

/// Selects retained construction and fixed-file removal owners.
pub trait Storage<C, L, P: Copy, R: Copy>: Removal {
    type Cleanup: memory::Cleanup<P>;
    type Release: memory::Cleanup<(R, Self::Slot)>;
}

/// Selects the native fixed-file target retained by a failed removal.
pub trait Removal {
    type Slot: Copy + Eq;
}

/// Driver-local work that must be drained before blocking.
pub trait Pending {
    fn has_deferred(&self) -> bool;
}

/// Runs the active application's phase after each native loan has ended.
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<()>;
}

/// Exact failure from native dispatch or completion decoding.
#[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 {
    /// The visible event queue was empty after synchronization.
    Drained,
    /// Visible events require another reactor turn.
    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,
        }
    }
}