dope-core 0.75.0

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

use crate::{
    driver::{
        self,
        schedule::{self, app},
    },
    io::{Event, event::Occupied},
};
use o3::{base::cell::brand, mem::slot};

use crate::executor::{self, drive::turn::scope::Core};

pub(crate) trait Mode<'d>: Sized {
    type Held;
    type Pending<'p>
    where
        'd: 'p;

    fn replay<'p>(
        &mut self,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p;

    fn emit<'p>(
        &mut self,
        event: Occupied<'_, 'd>,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p;

    fn park(core: &mut Core<'_, 'd, Self, impl crate::io::Report>);
}

pub(crate) struct App<'turn, 'app, 'd: 'app, D: executor::raw::Layout<'d>, S> {
    pub(crate) view: executor::raw::View<'app, 'd, D, S, D::ParkPlan>,
    pub(crate) token: &'turn mut brand::Token<brand::Identity<'d>>,
    pub(super) park: Option<app::Lease<'turn, 'd>>,
}

pub(crate) struct Boot;

impl<'app, 'd: 'app, D, S> Mode<'d> for App<'_, 'app, 'd, D, S>
where
    D: executor::raw::App<'d>,
{
    type Held = ();
    type Pending<'p>
        = &'p mut Option<Event<'d>>
    where
        'd: 'p;

    fn replay<'p>(
        &mut self,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        if let Some(event) = pending.take() {
            let mut output = MaybeUninit::uninit();
            self.emit(
                slot::Vacant::new(&mut output).insert(event),
                turn,
                driver,
                pending,
            )
        } else {
            ControlFlow::Continue(())
        }
    }

    fn emit<'p>(
        &mut self,
        event: Occupied<'_, 'd>,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        match self.view.dispatch(self.token, event, turn, driver) {
            ControlFlow::Continue(()) => ControlFlow::Continue(()),
            ControlFlow::Break(event) => {
                mem::forget(pending.replace(event.into_inner()));
                ControlFlow::Break(())
            }
        }
    }

    fn park(core: &mut Core<'_, 'd, Self, impl crate::io::Report>) {
        core.turn.expire();
        drop(core.mode.park.take());
        core.mode
            .view
            .pre_park(core.mode.token, core.turn.turn(), &mut *core.driver);
    }
}

impl<'d> Mode<'d> for Boot {
    type Held = Infallible;
    type Pending<'p>
        = ()
    where
        'd: 'p;

    fn replay<'p>(
        &mut self,
        _turn: schedule::Turn<'_, 'd>,
        _driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        _pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        ControlFlow::Continue(())
    }

    fn emit<'p>(
        &mut self,
        event: Occupied<'_, 'd>,
        _turn: schedule::Turn<'_, 'd>,
        _driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
        _pending: &mut Self::Pending<'p>,
    ) -> ControlFlow<Self::Held>
    where
        'd: 'p,
    {
        drop(event);
        ControlFlow::Continue(())
    }

    fn park(core: &mut Core<'_, 'd, Self, impl crate::io::Report>) {
        core.turn.expire();
    }
}

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