dope-core 0.75.0

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

use crate::driver::{self, schedule};
use crate::executor;
use o3::base::cell::region;

mod owner;
mod project;
mod resume;

pub use owner::Owner;

/// Lifecycle storage borrowing the exact pinned provider.
pub type Borrowed<'a, T> = Owner<pin::Pin<&'a mut T>, T>;
pub use project::Project;
pub use resume::Resume;

// SAFETY: the owned pair forwards each lifecycle operation to both exact pinned
// roots. Each root independently permits resume after stopped native quiescence;
// the pair adds no operations or cleanup awaiting dispatch between them.
unsafe impl<'d, A, B> Resume<'d> for crate::client::Pair<'d, A, B>
where
    A: crate::topology::lending::Root<'d> + Resume<'d>,
    B: crate::topology::lending::Root<'d> + Resume<'d>,
{
}

/// A dependent application retained by its pinned provider owner.
#[pin_project::pin_project]
#[derive(dope_gen::Application)]
#[dispatcher(core = crate, driver = 'd, lending = true)]
pub struct Bound<'app, 'd, H, T> {
    #[dispatcher(state)]
    driver: marker::PhantomData<driver::Reference<'d>>,
    #[pin]
    #[app]
    head: Borrowed<'app, H>,
    #[pin]
    #[app]
    tail: Owner<mem::ManuallyDrop<T>, T>,
}

impl<'app, 'd, H, T> Bound<'app, 'd, H, T> {
    pub(crate) const fn new(head: Borrowed<'app, H>, tail: T) -> Self {
        const {
            assert!(mem::size_of::<Self>() == mem::size_of::<(pin::Pin<&'app mut H>, T)>());
            assert!(mem::align_of::<Self>() == mem::align_of::<(pin::Pin<&'app mut H>, T)>());
            assert!(!mem::needs_drop::<Self>());
        }
        Self {
            driver: marker::PhantomData,
            head,
            tail: Owner::new(mem::ManuallyDrop::new(tail)),
        }
    }

    pub fn tail(self: pin::Pin<&Self>) -> pin::Pin<&T> {
        Project::shared(self.project_ref().tail)
    }
}

impl<'d, H, T> crate::topology::lending::Root<'d> for Bound<'_, 'd, H, T>
where
    T: crate::topology::lending::Root<'d>,
{
    type Provider = T::Provider;

    fn provider(self: pin::Pin<&Self>) -> pin::Pin<&Self::Provider> {
        crate::topology::lending::Root::provider(self.tail())
    }
}

// SAFETY: cooperative drain projects the same pinned dependent each time.
// Both applications remain installed through native quiescence and finish.
unsafe impl<'d, H, T> executor::raw::Drain<'d> for Bound<'_, 'd, H, T>
where
    Self: executor::raw::App<'d>,
    T: executor::raw::Drain<'d>,
{
    fn begin(
        self: pin::Pin<&mut Self>,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
    ) {
        executor::raw::Drain::begin(Project::exclusive(self.project().tail), turn, driver);
    }

    fn progress(
        self: pin::Pin<&Self>,
        region: &region::Token<region::Identity<'d>>,
        state: Option<&mut schedule::Waits<'d>>,
    ) -> ControlFlow<()> {
        executor::raw::Drain::progress(self.tail(), region, state)
    }

    fn force(
        self: pin::Pin<&mut Self>,
        turn: schedule::Turn<'_, 'd>,
        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
    ) {
        executor::raw::Drain::force(Project::exclusive(self.project().tail), turn, driver);
    }
}

impl<'parent, 'd, H, T> Bound<'parent, 'd, H, T>
where
    H: executor::raw::App<'d>,
    T: executor::raw::App<'d>,
{
    /// # Safety
    /// Consume this exact native-quiesced owner; never project or finish its backing again.
    pub(crate) unsafe fn finish_tail(
        app: pin::Pin<&mut Self>,
        install: driver::life::Install<'_, 'd>,
        finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
        plan: &executor::raw::Flat,
    ) -> <H as executor::raw::Layout<'d>>::ParkPlan {
        let app = app.project();
        <Owner<mem::ManuallyDrop<T>, T> as executor::raw::App<'d>>::finish(
            app.tail, install, finish,
        );
        let head = Project::shared(app.head.as_ref());
        // SAFETY: only the dependent was destroyed; the same pinned head is ready to resume.
        unsafe { <H::ParkPlan as executor::raw::Plan>::resume::<H>(head, plan) }
    }
}

const _: () = {
    assert!(mem::size_of::<Bound<'_, '_, (), ()>>() == mem::size_of::<&mut ()>());
    assert!(mem::align_of::<Bound<'_, '_, (), ()>>() == mem::align_of::<&mut ()>());
    assert!(!mem::needs_drop::<Bound<'_, '_, (), ()>>());
};

/// # Safety
/// Retire the same joined backing once, after native completion, preserving the pinned head.
pub unsafe trait Head<'d>: Sized {
    type Joined<T: executor::raw::App<'d>>: executor::raw::App<'d>;
    type Retired;

    fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T>;

    unsafe fn retire<T>(
        app: pin::Pin<&mut Self::Joined<T>>,
        install: driver::life::Install<'_, 'd>,
        finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
        plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
    ) -> Self::Retired
    where
        T: executor::raw::App<'d>;
}

unsafe impl<'d> Head<'d> for () {
    type Joined<T: executor::raw::App<'d>> = T;
    type Retired = ();

    fn join<T: executor::raw::App<'d>>(self, tail: T) -> T {
        tail
    }

    unsafe fn retire<T>(
        mut app: pin::Pin<&mut T>,
        install: driver::life::Install<'_, 'd>,
        finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
        _: &mut T::ParkPlan,
    ) where
        T: executor::raw::App<'d>,
    {
        T::finish(app.as_mut(), install, finish);
        unsafe { ptr::drop_in_place(app.get_unchecked_mut()) };
    }
}

unsafe impl<'parent, 'd: 'parent, H> Head<'d> for Borrowed<'parent, H>
where
    H: Resume<'d>,
{
    type Joined<T: executor::raw::App<'d>> = Bound<'parent, 'd, H, T>;
    type Retired = H::ParkPlan;

    fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T> {
        Bound::new(self, tail)
    }

    unsafe fn retire<T>(
        app: pin::Pin<&mut Self::Joined<T>>,
        install: driver::life::Install<'_, 'd>,
        finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
        plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
    ) -> Self::Retired
    where
        T: executor::raw::App<'d>,
    {
        unsafe { Bound::finish_tail(app, install, finish, plan) }
    }
}