dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use crate::driver::{
    life::domain,
    settings,
    settings::{claim, errors},
};
use crate::executor::{self, drive, session};
use crate::{driver, topology::lending};
use std::convert::Infallible;

pub trait Setup: settings::Resource + Sized {
    type Profile: settings::Profile;
    type Error;
    type NativeError;
    type Pending<'d>: 'd
    where
        Self: 'd;
    type Storage<'d>: Default
    where
        Self: 'd;
    type Port<'storage, 'd>: Bind<'d, Self::Pending<'d>, Error = Self::NativeError> + 'storage
    where
        Self: 'd,
        'd: 'storage;

    fn config(&self) -> Result<settings::Config, errors::Claim> {
        claim::Needs::from_tree(self)?.apply_to(settings::Config::for_profile::<Self::Profile>())
    }

    fn prepare<'storage, 'd: 'storage>(
        self,
        storage: &'storage mut Self::Storage<'d>,
        driver: &mut driver::Local<'d>,
    ) -> Result<(Self::Port<'storage, 'd>, Self::Pending<'d>), Self::Error>
    where
        Self: 'd;
}

pub trait Bind<'d, Pending: 'd> {
    type Error;
    type Root<'port>: lending::Root<'d> + lending::raw::Resume<'d>
    where
        Self: 'port,
        'd: 'port;

    fn bind<'port>(
        &'port self,
        pending: Pending,
        driver: &mut driver::Local<'d>,
    ) -> Result<Self::Root<'port>, Self::Error>
    where
        'd: 'port;
}

#[derive(Debug)]
pub enum Build<E, N> {
    Prepare(E),
    Native(N),
}

type Observation<'a, R> = Result<R, (domain::Unmap, &'a R)>;

type Run<B, T, E, X> =
    Result<(Result<T, E>, Option<session::Shutdown>), lending::Error<B, Infallible, X>>;

#[derive(Debug)]
pub enum Cause<'a, B, E, X> {
    Build(&'a B),
    Create(executor::Kind<'a>),
    Output(&'a E),
    Session(session::Cause<'a, X>),
    Close {
        failure: domain::Unmap,
        retained: Result<Result<(), &'a E>, crate::BranchError<&'a B, session::Cause<'a, X>>>,
    },
}

pub struct Diagnostic<'a, B, T, E = drive::Error, X = Infallible> {
    pub cause: Cause<'a, B, E, X>,
    pub output: Option<Result<&'a T, &'a E>>,
}

impl<'a, B, T, E, X> Diagnostic<'a, B, T, E, X> {
    pub fn build(cause: &'a B) -> Self {
        Self {
            cause: Cause::Build(cause),
            output: None,
        }
    }

    pub fn create(cause: &'a executor::Error<'_>) -> Self {
        Self {
            cause: Cause::Create(cause.kind()),
            output: None,
        }
    }

    pub fn finish<Report, R>(
        report: &mut Report,
        result: Observation<'_, Run<B, T, E, X>>,
        finish: impl FnOnce(&mut Report, Result<T, Diagnostic<'_, B, T, E, X>>) -> R,
    ) -> R {
        let error = match result {
            Ok(Ok((Ok(value), None))) => return finish(report, Ok(value)),
            Ok(Ok((Err(ref cause), None))) => Diagnostic {
                cause: Cause::Output(cause),
                output: Some(Err(cause)),
            },
            Ok(Ok((ref output, Some(ref shutdown)))) => Diagnostic {
                cause: Cause::Session(shutdown.cause()),
                output: Some(output.as_ref()),
            },
            Ok(Err(lending::Error::Build(ref cause))) => Diagnostic::build(cause),
            Ok(Err(lending::Error::Run(ref cause))) => Diagnostic {
                cause: Cause::Session(cause.cause()),
                output: None,
            },
            Err((failure, run)) => {
                let (retained, output) = match run {
                    Ok((output, shutdown)) => {
                        let output = output.as_ref();
                        let retained = match shutdown {
                            None => Ok(output.err().map_or(Ok(()), Err)),
                            Some(shutdown) => Err(crate::BranchError::Second(shutdown.cause())),
                        };
                        (retained, Some(output))
                    }
                    Err(lending::Error::Build(cause)) => {
                        (Err(crate::BranchError::First(cause)), None)
                    }
                    Err(lending::Error::Run(cause)) => {
                        (Err(crate::BranchError::Second(cause.cause())), None)
                    }
                };
                Diagnostic {
                    cause: Cause::Close { failure, retained },
                    output,
                }
            }
        };
        finish(report, Err(error))
    }
}