dope-core 0.75.0

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

use o3::{base::cell::brand, buffer};
#[cfg(target_os = "macos")]
use o3::{
    base::cell::region,
    buffer::storage::provided::{self, fixed},
};

#[cfg(target_os = "macos")]
use crate::Engine;
use crate::driver::{
    self,
    life::{Operation, Root, domain, epochs::Closed},
    storage,
};

pub(super) struct Provided<O>(pub(super) O);

#[cfg(target_os = "linux")]
impl<O> Provided<O> {
    pub(super) fn run<A, Q: ?Sized, Report: crate::io::Report>(
        driver: &mut driver::Driver,
        arenas: A,
        source: &domain::Source<Q>,
        op: O,
        report: &mut Report,
    ) -> O::Output
    where
        A: buffer::raw::Arenas,
        O: Operation<storage::Scope<A>, Q, Report>,
    {
        use dope_sys::memory::Context as _;
        let runtime = &mut driver.runtime;
        let accounts = runtime.shared.context().buffer().layout().0.accounts();
        let root = Root {
            flights: &mut runtime.flights,
            resident: &mut runtime.resident,
            source,
            report,
        };
        let input = (root, arenas, &mut runtime.shared, &mut runtime.backend);
        let output = brand::scope_pinned(pin::pin!(input), |token, regions, input| {
            let (root, arenas, shared, backend) = input.get_mut();
            // SAFETY: this sole generation owns the inactive mapping and its terminal.
            let shared = unsafe { shared.enter(accounts) };
            let (state, source) = root.enter(shared, token, regions, &mut **backend);
            // SAFETY: the same generation retains every arena through native retirement.
            let (roots, finish) = unsafe { arenas.enter() };
            let output = {
                let store = pin::pin!(roots);
                Closed::run::<storage::Scope<A>, _, _, _>(state, store.as_ref(), source, op).0
            };
            finish();
            output
        });
        // SAFETY: Closed::run ended all native access; the generative callback
        // and all issued borrows have ended, including forgotten account edges.
        unsafe { runtime.shared.refund_ended() };
        output
    }
}

#[cfg(target_os = "macos")]
impl<A, Q: ?Sized, O, Report: crate::io::Report>
    fixed::Operation<(Root<'_, Q, Report>, A), crate::Buffer, driver::Shared> for Provided<O>
where
    A: buffer::raw::Arenas,
    O: Operation<storage::Scope<A>, Q, Report>,
{
    type Output = O::Output;
    type Terminal = crate::Engine;
    type Proof = Closed;

    fn run<'d>(
        self,
        token: brand::Token<brand::Identity<'d>>,
        regions: (
            region::Token<region::Identity<'d>>,
            region::Token<region::Identity<'d, 1>>,
        ),
        shared: provided::Root<'d, crate::Buffer, driver::Shared, ()>,
        root: pin::Pin<&'d mut (Root<'_, Q, Report>, A)>,
        backend: fixed::Loan<'d, Self::Terminal>,
    ) -> (Self::Output, fixed::End<'d, Self::Terminal, Self::Proof>)
    where
        Self: 'd,
    {
        let (root, arenas) = root.get_mut();
        let (state, source) = root.enter(shared, token, regions, backend);
        // SAFETY: the same generative borrow owns both the arenas and driver.
        // Output cannot retain 'd; reclamation precedes resident and backend teardown.
        let (roots, finish) = unsafe { arenas.enter() };
        let (output, end) = {
            let store = pin::pin!(roots);
            Closed::run::<storage::Scope<A>, _, _, _>(state, store.as_ref(), source, self.0)
        };
        finish();
        (output, end)
    }
}

pub(super) struct Build<S, O> {
    pub(super) plan: S,
    pub(super) operation: O,
}

#[cfg(target_os = "linux")]
impl<S: storage::Build, O> Build<S, O> {
    pub(super) fn run<Q: ?Sized, Report: crate::io::Report>(
        self,
        driver: &mut driver::Driver,
        source: &domain::Source<Q>,
        report: &mut Report,
    ) -> Result<O::Output, domain::Rejected<S::Error, O>>
    where
        O: Operation<S, Q, Report>,
    {
        use dope_sys::memory::Context as _;
        let runtime = &mut driver.runtime;
        let accounts = runtime.shared.context().buffer().layout().0.accounts();
        let root = Root {
            flights: &mut runtime.flights,
            resident: &mut runtime.resident,
            source,
            report,
        };
        let input = (root, &mut runtime.shared, &mut runtime.backend);
        let output = brand::scope_pinned(pin::pin!(input), |token, regions, input| {
            let (root, shared, backend) = input.get_mut();
            // SAFETY: this sole generation owns the inactive mapping and its terminal.
            let shared = unsafe { shared.enter(accounts) };
            let (mut state, source) = root.enter(shared, token, regions, &mut **backend);
            let (mut backing, owned) = match self.plan.try_store() {
                Ok(backing) => backing,
                Err(cause) => {
                    return Closed::reject::<S, _, _, _, _>(state, source, cause, self.operation).0;
                }
            };
            match self.plan.try_build(
                owned,
                &mut backing,
                storage::Context::new(&mut state.lifecycle.local),
            ) {
                Ok(value) => {
                    let store = pin::pin!(value);
                    Ok(Closed::run::<S, _, _, _>(state, store.as_ref(), source, self.operation).0)
                }
                Err(cause) => {
                    Closed::reject::<S, _, _, _, _>(state, source, cause, self.operation).0
                }
            }
        });
        // SAFETY: both build outcomes retired native state before this generation ended.
        unsafe { runtime.shared.refund_ended() };
        output
    }
}

#[cfg(target_os = "macos")]
impl<Q: ?Sized, S: storage::Build, Report: crate::io::Report, O: Operation<S, Q, Report>>
    fixed::Operation<Root<'_, Q, Report>, crate::Buffer, driver::Shared> for Build<S, O>
{
    type Output = Result<O::Output, domain::Rejected<S::Error, O>>;
    type Terminal = Engine;
    type Proof = Closed;

    fn run<'d>(
        self,
        token: brand::Token<brand::Identity<'d>>,
        regions: (
            region::Token<region::Identity<'d>>,
            region::Token<region::Identity<'d, 1>>,
        ),
        shared: provided::Root<'d, crate::Buffer, driver::Shared, ()>,
        root: pin::Pin<&'d mut Root<'_, Q, Report>>,
        backend: fixed::Loan<'d, Engine>,
    ) -> (Self::Output, fixed::End<'d, Self::Terminal, Self::Proof>)
    where
        Self: 'd,
    {
        let root = root.get_mut();
        let (mut state, source) = root.enter(shared, token, regions, backend);
        let prepared = self.plan.try_store();
        let (mut backing, owned) = match prepared {
            Ok(backing) => backing,
            Err(error) => {
                return Closed::reject::<S, _, _, _, _>(state, source, error, self.operation);
            }
        };
        let built = self.plan.try_build(
            owned,
            &mut backing,
            storage::Context::new(&mut state.lifecycle.local),
        );
        match built {
            Ok(value) => {
                let store = pin::pin!(value);
                let (output, end) =
                    Closed::run::<S, _, _, _>(state, store.as_ref(), source, self.operation);
                (Ok(output), end)
            }
            Err(cause) => Closed::reject::<S, _, _, _, _>(state, source, cause, self.operation),
        }
    }
}

pub(super) struct Direct<S, O> {
    pub(super) plan: S,
    pub(super) operation: O,
}

#[cfg(target_os = "linux")]
impl<S: storage::Direct, O> Direct<S, O> {
    pub(super) fn run<Q: ?Sized, Report: crate::io::Report>(
        self,
        driver: &mut driver::Driver,
        source: &domain::Source<Q>,
        report: &mut Report,
    ) -> O::Output
    where
        O: Operation<S, Q, Report>,
    {
        use dope_sys::memory::Context as _;
        let runtime = &mut driver.runtime;
        let accounts = runtime.shared.context().buffer().layout().0.accounts();
        let root = Root {
            flights: &mut runtime.flights,
            resident: &mut runtime.resident,
            source,
            report,
        };
        let input = (root, &mut runtime.shared, &mut runtime.backend);
        let output = brand::scope_pinned(pin::pin!(input), |token, regions, input| {
            let (root, shared, backend) = input.get_mut();
            // SAFETY: this sole generation owns the inactive mapping and its terminal.
            let shared = unsafe { shared.enter(accounts) };
            let (state, source) = root.enter(shared, token, regions, &mut **backend);
            let store = pin::pin!(storage::Direct::build(self.plan));
            Closed::run::<S, _, _, _>(state, store.as_ref(), source, self.operation).0
        });
        // SAFETY: native retirement and every issued borrow ended in the callback.
        unsafe { runtime.shared.refund_ended() };
        output
    }
}

#[cfg(target_os = "macos")]
impl<Q: ?Sized, S: storage::Direct, Report: crate::io::Report, O: Operation<S, Q, Report>>
    fixed::Operation<Root<'_, Q, Report>, crate::Buffer, driver::Shared> for Direct<S, O>
{
    type Output = O::Output;
    type Terminal = Engine;
    type Proof = Closed;

    fn run<'d>(
        self,
        token: brand::Token<brand::Identity<'d>>,
        regions: (
            region::Token<region::Identity<'d>>,
            region::Token<region::Identity<'d, 1>>,
        ),
        shared: provided::Root<'d, crate::Buffer, driver::Shared, ()>,
        root: pin::Pin<&'d mut Root<'_, Q, Report>>,
        backend: fixed::Loan<'d, Engine>,
    ) -> (Self::Output, fixed::End<'d, Self::Terminal, Self::Proof>)
    where
        Self: 'd,
    {
        let root = root.get_mut();
        let (state, source) = root.enter(shared, token, regions, backend);
        let value = storage::Direct::build(self.plan);
        let store = pin::pin!(value);
        Closed::run::<S, _, _, _>(state, store.as_ref(), source, self.operation)
    }
}