dope-core 0.77.0

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

use crate::{executor, topology::lending};
use o3::base::cell::{brand, region};

#[pin_project::pin_project(project_ref = StoredRef)]
#[repr(C)]
pub(crate) struct Stored<D, Plan> {
    #[pin]
    pub(crate) app: D,
    pub(crate) park: Plan,
}

pub(crate) struct Backing<'d, D, S = mem::ManuallyDrop<D>>
where
    D: executor::raw::Layout<'d>,
    S: lending::raw::Project<App = D>,
{
    value: mem::ManuallyDrop<region::Value<brand::Identity<'d>, Stored<S, D::ParkPlan>>>,
}

impl<'d, D: executor::raw::App<'d>> Backing<'d, D> {
    pub(crate) fn new(token: &region::Token<brand::Identity<'d>>, value: D) -> Self {
        Self::prepared(token, mem::ManuallyDrop::new(value), D::ParkPlan::default())
    }
}

impl<'d, D, S> Backing<'d, D, S>
where
    D: executor::raw::Layout<'d>,
    S: lending::raw::Project<App = D>,
{
    pub(crate) fn prepared(
        token: &region::Token<brand::Identity<'d>>,
        app: S,
        park: D::ParkPlan,
    ) -> Self {
        let stored = Stored { app, park };
        Self {
            value: mem::ManuallyDrop::new(token.bind(stored)),
        }
    }

    pub(crate) fn pin(
        self: pin::Pin<&Self>,
    ) -> pin::Pin<&region::Value<brand::Identity<'d>, Stored<S, D::ParkPlan>>> {
        // SAFETY: the source Pin prevents Backing from moving, and ManuallyDrop
        // keeps the exact Value allocation live until the owner finishes.
        unsafe { self.map_unchecked(|backing| &*backing.value) }
    }
}