dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use std::{ops::ControlFlow, pin, time};

use drive::{clock, serve, turn};
use o3::base::{cell::region, time::Positive};
use turn::scope;

use crate::{
    client,
    executor::{self, drain, drive, root, session},
    topology::lending,
};

/// Borrowed capabilities shared by every live application view.
pub struct Access<
    'app,
    'life: 'app,
    'd: 'life,
    D,
    Report,
    Q = (),
    Plan = <D as executor::raw::Layout<'d>>::ParkPlan,
> {
    pub(crate) core: &'app mut session::Core<'life, 'd, Q, Report>,
    view: executor::raw::View<'app, 'd, D, executor::raw::Live, Plan>,
}

impl<'app, 'life: 'app, 'd: 'life, D, Q, Plan, Report: crate::io::Report>
    Access<'app, 'life, 'd, D, Report, Q, Plan>
{
    pub(crate) fn reborrow(&mut self) -> Access<'_, 'life, 'd, D, Report, Q, Plan> {
        Access {
            core: &mut *self.core,
            view: self.view,
        }
    }

    pub fn state_token(&mut self) -> &mut region::Token<region::Identity<'d, 1>> {
        &mut self.core.scope.context().regions_mut().1
    }

    /// Issues the client of the complete installed root.
    pub fn root_client(&mut self) -> <D::Provider as client::Provider<'d>>::Client<'app>
    where
        D: lending::Root<'d>,
        D::Provider: 'app,
    {
        let root = self.view.app(self.core.scope.token());
        let provider = lending::Root::provider(root);
        client::Provider::provide(client::Context::new(provider))
    }

    pub fn client<P>(
        &mut self,
        select: impl for<'borrow> FnOnce(pin::Pin<&'borrow D>) -> pin::Pin<&'borrow P>,
    ) -> <P::Provider as client::Provider<'d>>::Client<'app>
    where
        P: lending::Root<'d>,
        P::Provider: 'app,
    {
        let root = select(self.view.app(self.core.scope.token()));
        let provider = lending::Root::provider(root);
        client::Provider::provide(client::Context::new(provider))
    }

    pub fn inspect(&mut self) -> pin::Pin<&D> {
        self.view.app(self.core.scope.token())
    }
}

impl<'app, 'life: 'app, 'd: 'life, D, Q, Plan, Report: crate::io::Report>
    Access<'app, 'life, 'd, D, Report, Q, Plan>
where
    D: executor::raw::App<'d, ParkPlan = Plan>,
    Q: executor::Startup,
{
    pub(super) const fn new(
        core: &'app mut session::Core<'life, 'd, Q, Report>,
        view: executor::raw::View<'app, 'd, D, executor::raw::Live, Plan>,
    ) -> Self {
        Self { core, view }
    }

    pub(super) fn drive<R, C>(self, source: C, root: R) -> Result<R::Output, drive::Error>
    where
        R: root::Root<'d>,
        C: clock::Time,
    {
        let observer = self.core.lifecycle;
        self.drive_with(source, root, observer)
    }

    pub(crate) fn drive_with<R, C, O>(
        self,
        source: C,
        root: R,
        observer: &O,
    ) -> Result<R::Output, drive::Error>
    where
        R: root::Root<'d>,
        C: clock::Time,
        O: executor::Startup<Q>,
    {
        let state = self.scope();
        state.run::<R, C, Q, O>(source, root, observer)
    }

    pub(super) fn drive_fixed<R, C>(
        self,
        source: C,
        root: R,
    ) -> Result<session::Fixed<'d, R::Output>, drive::Error>
    where
        R: root::Root<'d>,
        C: clock::Time<Proof<'d> = crate::driver::schedule::clock::Moment<'d>>,
    {
        let observer = self.core.lifecycle;
        let state = self.scope();
        state.run_fixed(source, root, observer)
    }

    pub(super) fn run(self) -> Result<(), drive::Error> {
        let observer = self.core.lifecycle;
        serve::run(self.scope(), clock::wall(), observer)
    }

    pub(super) fn run_for(self, timeout: time::Duration) -> Result<(), drive::Error> {
        let source = clock::Bound::until(clock::wall(), timeout)?;
        let observer = self.core.lifecycle;
        serve::run(self.scope(), source, observer)
    }

    pub(super) fn drive_until<R>(self, root: R) -> Result<ControlFlow<R::Output>, drive::Error>
    where
        R: root::Root<'d>,
    {
        let observer = self.core.lifecycle;
        let state = self.scope();
        state.run_until(clock::wall(), root, observer)
    }

    pub(super) fn run_drain(mut self, timeout: Positive) -> Result<drain::Outcome, drive::Error>
    where
        D: executor::raw::Drain<'d>,
    {
        self.reborrow().run()?;
        self.drain(timeout)
    }

    pub(super) fn drive_drain<R>(
        mut self,
        timeout: Positive,
        root: R,
    ) -> Result<ControlFlow<R::Output, drain::Outcome>, drive::Error>
    where
        D: executor::raw::Drain<'d>,
        R: root::Root<'d>,
    {
        match self.reborrow().drive_until(root)? {
            ControlFlow::Break(output) => Ok(ControlFlow::Break(output)),
            ControlFlow::Continue(()) => self.drain(timeout).map(ControlFlow::Continue),
        }
    }

    fn drain(self, timeout: Positive) -> Result<drain::Outcome, drive::Error>
    where
        D: executor::raw::Drain<'d>,
    {
        let source = clock::Bound::until(clock::wall(), timeout.get())?;
        let observer = self.core.lifecycle;
        drain::run(self.scope(), source, observer)
    }

    fn scope(self) -> scope::Scope<'app, 'app, 'd, D, executor::raw::Live, Report> {
        scope::Scope::new(&mut self.core.scope, self.view, &mut self.core.pending)
    }
}