dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::{
    convert::Infallible,
    mem::{self, MaybeUninit},
    ops::ControlFlow,
    pin,
};

use dope_sys::{engine::Mode, io::reactor::event::Report};
use o3::{
    base::cell::{brand, region},
    mem::slot,
};

use crate::{
    driver::schedule::{self, app},
    executor::{self, drive::turn::scope::Core},
    io::event::{Dispatch, Output},
    topology::lending,
};

pub struct App<
    'turn,
    'app,
    'd: 'app,
    D: executor::raw::Layout<'d>,
    P: lending::raw::Project<App = D> = mem::ManuallyDrop<D>,
    S = Option<app::Lease<'turn, 'd>>,
> {
    pub(crate) stored:
        pin::Pin<&'app region::Value<brand::Identity<'d>, executor::raw::Stored<P, D::ParkPlan>>>,
    pub(crate) token: &'turn mut region::Token<brand::Identity<'d>>,
    pub(crate) state: S,
}

pub(crate) struct Boot;

impl<'scope, 'turn, 'app, 'd: 'app, D, P, R, Q, O> Mode<'d, Q, O>
    for Core<'scope, 'd, App<'turn, 'app, 'd, D, P>, R>
where
    D: executor::raw::App<'d>,
    P: lending::raw::Project<App = D>,
    R: Report<Event = crate::Reported>,
    O: executor::Startup<Q>,
{
    type Event = Output<'d>;
    type Held = ();
    type Work = schedule::Turn<'scope, 'd>;
    type Pending<'p>
        = &'p mut Option<Self::Event>
    where
        'd: 'p;

    fn replay<'p>(
        &mut self,
        work: Self::Work,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        if let Some(event) = pending.take() {
            let mut output = MaybeUninit::uninit();
            <Self as Mode<'d, Q, O>>::emit(
                self,
                slot::raw::Vacant::new(&mut output).insert(event),
                work,
                pending,
            )
        } else {
            ControlFlow::Continue(())
        }
    }

    fn emit<'p>(
        &mut self,
        event: slot::Occupied<'_, Self::Event>,
        work: Self::Work,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        let app = lending::raw::Project::exclusive(
            self.mode.token.pin_mut(self.mode.stored).project().app,
        );
        let event = match Dispatch::new(event).scoped::<D::Scope>() {
            Ok(event) => match D::dispatch(app, event, work, &mut *self.driver) {
                ControlFlow::Continue(()) => return ControlFlow::Continue(()),
                ControlFlow::Break(event) => event.into_event(),
            },
            Err(event) => event.into_event(),
        };
        mem::forget(pending.replace(event.into_inner()));
        ControlFlow::Break(())
    }

    fn park(&mut self) {
        self.turn.expire();
        drop(self.mode.state.take());
        self.park_native();
    }

    fn observed(observer: &O) -> ControlFlow<()> {
        observer.observed(executor::Requested::<Q>::new())
    }
}

impl<'scope, 'd, R, Q, O> Mode<'d, Q, O> for Core<'scope, 'd, Boot, R>
where
    R: Report<Event = crate::Reported>,
    O: executor::Startup<Q>,
{
    type Event = Output<'d>;
    type Held = Infallible;
    type Work = schedule::Turn<'scope, 'd>;
    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,
    {
        ControlFlow::Continue(())
    }

    fn emit<'p>(
        &mut self,
        event: slot::Occupied<'_, Self::Event>,
        _work: Self::Work,
        _pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        drop(event);
        ControlFlow::Continue(())
    }

    fn park(&mut self) {
        self.turn.expire();
    }

    fn observed(observer: &O) -> ControlFlow<()> {
        observer.observed(executor::Requested::<Q>::new())
    }
}

const _: () = assert!(mem::size_of::<Boot>() == 0);