dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use std::{convert, marker, num::NonZero, ops::ControlFlow};

use crate::driver::{
    life,
    life::{creation, domain},
    settings, storage,
};

use o3::base::marker::ThreadBound;

pub mod boot;
pub mod create;
pub mod drain;
pub mod drive;
pub mod raw;
pub mod root;
pub mod session;
#[doc(hidden)]
pub mod work;
pub trait Op<S: storage::Plan, Q: Startup, Report: crate::io::Report>: Sized {
    type Output;

    fn run<'scope, 'life, 'p, 'd>(
        self,
        storage: storage::Loan<'life, 'p, 'd, S>,
        session: session::Session<'scope, 'life, 'd, Q, Report>,
    ) -> Self::Output
    where
        'd: 'p,
        'p: 'life,
        'life: 'scope,
        Self: 'd,
        S: 'd,
        Q: 'd;
}

struct Entry<O>(O);

impl<S, Q, O, Report: crate::io::Report> life::Operation<S, Q, Report> for Entry<O>
where
    S: storage::Plan,
    Q: Startup,
    O: Op<S, Q, Report>,
{
    type Output = O::Output;

    fn rejected(&self, source: &Q) {
        source.failed();
    }

    fn run<'life, 'p, 'd>(
        self,
        scope: life::Scope<'life, 'd, Report>,
        storage: storage::Loan<'life, 'p, 'd, S>,
        source: &'life Q,
    ) -> Self::Output
    where
        'd: 'p,
        'p: 'life,
        Self: 'd,
        S: 'd,
        Q: 'd,
    {
        let mut core = session::Core::new(scope, source);
        self.0.run(storage, session::Session::new(&mut core))
    }
}

pub fn enter<'owner, S, Q, O, Report: crate::io::Report>(
    generation: domain::Generation<Q>,
    teardown: &'owner mut domain::Teardown,
    storage: S,
    operation: O,
    report: &mut Report,
) -> Result<O::Output, domain::Error<'owner, convert::Infallible, O::Output, Q>>
where
    S: storage::Direct,
    Q: Startup,
    O: Op<S, Q, Report>,
{
    generation
        .start(teardown)
        .enter(storage, Entry(operation), report)
}

pub fn scope<'owner, A, Q, O, Report: crate::io::Report>(
    generation: domain::Generation<Q>,
    teardown: &'owner mut domain::Teardown,
    storage: storage::Scope<A>,
    operation: O,
    report: &mut Report,
) -> Result<O::Output, domain::Error<'owner, convert::Infallible, O::Output, Q>>
where
    A: o3::buffer::raw::Arenas,
    Q: Startup,
    O: Op<storage::Scope<A>, Q, Report>,
{
    generation
        .start(teardown)
        .scope(storage, Entry(operation), report)
}

/// Lifecycle behavior selected by an executor's shutdown source.
pub trait Startup<Q = Self>: Sized {
    type Error;
    const INTEREST: crate::dope_sys::engine::register::Interest =
        crate::dope_sys::engine::register::Interest::Readable;

    /// Publishes installation readiness or preserves its exact notification failure.
    fn installed(&self) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Observes the registered shutdown source becoming ready.
    fn observed(&self, _request: Requested<Q>) -> ControlFlow<()> {
        ControlFlow::Break(())
    }

    /// Records installation failure before native cleanup.
    fn failed(&self) {}
}

impl Startup for () {
    type Error = convert::Infallible;
}

#[must_use]
pub struct Requested<Q>(marker::PhantomData<(fn(Q) -> Q, ThreadBound)>);

impl<Q> Requested<Q> {
    pub(crate) const fn new() -> Self {
        Self(marker::PhantomData)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InstallError<E = convert::Infallible> {
    Claim(settings::errors::Claim),
    Coordinators { count: NonZero<usize>, limit: usize },
    CoordinatorCountOverflow,
    Startup(E),
}

impl<E> From<settings::errors::Claim> for InstallError<E> {
    fn from(error: settings::errors::Claim) -> Self {
        Self::Claim(error)
    }
}

pub trait Build: Sized {
    type Shutdown: Startup;

    fn try_build(
        self,
        attempt: &mut creation::Attempt,
        config: settings::Config,
    ) -> Result<domain::Generation<Self::Shutdown>, create::Error<'_>>;
}

impl Build for () {
    type Shutdown = ();

    fn try_build(
        self,
        attempt: &mut creation::Attempt,
        config: settings::Config,
    ) -> Result<domain::Generation, create::Error<'_>> {
        Ok(domain::Generation::new(attempt.try_driver(config)?))
    }
}

pub fn try_enter<'owner, S, Q, O, Report: crate::io::Report>(
    generation: domain::Generation<Q>,
    teardown: &'owner mut domain::Teardown,
    storage: S,
    operation: O,
    report: &mut Report,
) -> Result<O::Output, domain::Error<'owner, domain::Rejected<S::Error, O>, O::Output, Q>>
where
    S: storage::Build,
    Q: Startup,
    O: Op<S, Q, Report>,
{
    generation
        .start(teardown)
        .try_enter(storage, Entry(operation), report)
        .map_err(|error| error.map_storage(|rejected| rejected.map_op(|entry| entry.0)))
}