dope-manifold 0.28.0

Owned application and service composition for dope
Documentation
use std::pin;

use dope_core::driver::{
    route::{self, Identity, kind},
    schedule,
};
use o3::buffer::bytes::{Prefix, bounded};

use crate::{
    app::{dispatch, dispatch::event},
    file::appender::{self, state::State},
};

pub(super) fn bytes<'a, const N: usize>(
    pending: &'a appender::Pending<'_, N>,
) -> Prefix<'a, { bounded::MAX }> {
    let bytes = pending.bytes();
    // SAFETY: new bounds remaining by this unchanged exclusive lease's length.
    // advance only reduces remaining, whose Len bound also proves bounded::MAX.
    // release consumes the owner, so the admitted extent stays unchanged.
    unsafe {
        Prefix::from_raw(bounded::raw::Input::new(
            bytes.get_unchecked(bytes.len() - pending.remaining().into_raw()..),
        ))
    }
}

// SAFETY: Appender is installed pinned and owns its descriptor and fixed blocks,
// and flight slots through shutdown drain and finish.
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Manifold<'d>
    for appender::Appender<'p, 'd, ID, N, B>
{
    type Route = <ID as route::Materialize<kind::Write>>::With;
    type Dispatch = event::Driven<dispatch::raw::Retained>;
    type Install = dispatch::raw::Omit;
    type Finish = Self;
    type Activate = dispatch::raw::Omit;
    type Probe = Self;
    type PrePark = dispatch::raw::Retained;
    type Shutdown = dispatch::raw::Plain;

    const COORDS: usize = 0;

    unsafe fn pre_park<'a, 'turn>(
        mut self: pin::Pin<&'a mut Self>,
        _: schedule::Turn<'turn, 'd>,
        driver: &mut dispatch::raw::Context<'_, 'd, Self::PrePark>,
    ) -> pin::Pin<&'a mut Self> {
        let this = self.as_mut().get_mut();
        if let Some(pending) = this.prepare() {
            this.submit(pending, driver);
        }
        self
    }

    fn shutdown<'a, 'turn>(
        mut self: pin::Pin<&'a mut Self>,
        _: schedule::Turn<'turn, 'd>,
        _: &mut dispatch::raw::Context<'_, 'd, Self::Shutdown>,
    ) -> pin::Pin<&'a mut Self> {
        self.as_mut().get_mut().close();
        self
    }
}

// SAFETY: unit selection binds every query to this exact pinned appender.
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Probe<'d, Self>
    for appender::Appender<'p, 'd, ID, N, B>
{
    type Plan = ();

    fn select() -> Option<Self::Plan> {
        Some(())
    }

    fn run(call: dispatch::raw::Call<'_, '_, '_, 'd, Self, ()>) -> schedule::Progress<'d> {
        match call.owner().get_ref().progress() {
            State::Runnable => schedule::Progress::Runnable,
            State::Waiting => schedule::Progress::waiting(call.region()),
            State::Quiescent => schedule::Progress::Quiescent,
        }
    }
}

// SAFETY: Control keeps the owner pinned, copies only into idle blocks, and
// replaces the file only after every native write loan has ended.
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Control<'d>
    for appender::Appender<'p, 'd, ID, N, B>
{
    type View<'step>
        = appender::Control<'step, 'p, 'd, ID, N, B>
    where
        Self: 'step,
        'd: 'step;

    fn view<'step>(owner: dispatch::raw::Installed<'step, 'd, Self>) -> Self::View<'step>
    where
        'd: 'step,
    {
        appender::Control { inner: owner }
    }
}