dope-core 0.75.0

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

use crate::driver::{self, schedule};

use super::{App, Count};

#[derive(Default)]
pub struct Census {
    parks: usize,
    groups: usize,
    coords: usize,
}

impl Census {
    pub fn coordinate(&mut self) -> Option<()> {
        self.groups = self.groups.checked_add(1)?;
        self.coords = self.coords.checked_add(1)?;
        Some(())
    }

    pub(super) fn include_leaf(&mut self, coords: usize) -> Option<()> {
        self.parks = self.parks.checked_add(1)?;
        self.groups = self.groups.checked_add(usize::from(coords != 0))?;
        self.coords = self.coords.checked_add(coords)?;
        Some(())
    }
}

#[derive(Default)]
pub struct Flat {
    parks: usize,
    next: [usize; 2],
    groups: u32,
    coord: u32,
}

mod sealed {
    pub trait Plan {}
    impl Plan for () {}
    impl Plan for super::Flat {}
}

pub trait Plan: sealed::Plan + Default {
    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count>;

    /// # Safety
    /// Run the same admitted pinned root with its unchanged census and turn.
    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
        &mut self,
        app: pin::Pin<&'app mut D>,
        turn: schedule::Turn<'turn, 'd>,
        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
    ) where
        'd: 'driver + 'turn,
        D: App<'d, ParkPlan = Self>;

    /// # Safety
    /// Resume the unchanged head after its joined root has finished natively.
    /// An unadmitted joined root must finish without running this plan.
    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(app: pin::Pin<&D>, prior: &Flat) -> Self;
}

impl Plan for () {
    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count> {
        let mut count = Census::default();
        D::census(app, &mut count)?;
        Some(Count::new(count.coords))
    }

    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
        &mut self,
        app: pin::Pin<&'app mut D>,
        turn: schedule::Turn<'turn, 'd>,
        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
    ) where
        'd: 'driver + 'turn,
        D: App<'d, ParkPlan = Self>,
    {
        // SAFETY: the installed owner carries this root's admitted turn.
        unsafe { D::park(app, turn, driver) };
    }

    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(_: pin::Pin<&D>, _: &Flat) -> Self {}
}

impl Plan for Flat {
    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count> {
        let mut count = Census::default();
        D::census(app, &mut count)?;
        self.parks = count.parks;
        // Admission bounds every coordinator group by the same total count.
        self.groups = count.groups as u32;
        Some(Count::new(count.coords))
    }

    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
        &mut self,
        app: pin::Pin<&'app mut D>,
        turn: schedule::Turn<'turn, 'd>,
        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
    ) where
        'd: 'driver + 'turn,
        D: App<'d, ParkPlan = Self>,
    {
        let ahead = [
            start(&mut self.next[0], turn.app().remaining(), self.parks),
            start(&mut self.next[1], turn.maint().remaining(), self.parks),
        ];
        let mut next = self.coord as usize;
        let coord = start(&mut next, turn.coord().remaining(), self.groups as usize);
        self.coord = next as u32;
        let mut sweep = Sweep {
            turn,
            driver,
            parks: self.parks,
            ahead,
            groups: self.groups,
            coord: coord as u32,
        };
        // SAFETY: preparation admitted the same pinned traversal and its counts.
        unsafe { D::sweep(app, &mut sweep) };
    }

    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(app: pin::Pin<&D>, prior: &Flat) -> Self {
        let mut plan = Self::default();
        // A live head is an unchanged subset of the admitted joined root.
        let _ = plan.prepare(app);
        plan.next = [
            if prior.next[0] < plan.parks {
                prior.next[0]
            } else {
                0
            },
            if prior.next[1] < plan.parks {
                prior.next[1]
            } else {
                0
            },
        ];
        plan.coord = if prior.coord < plan.groups {
            prior.coord
        } else {
            0
        };
        plan
    }
}

fn start(next: &mut usize, credits: usize, count: usize) -> usize {
    if credits == 0 || count <= 1 {
        return 0;
    }
    let at = *next;
    let following = at.wrapping_add(1);
    *next = if following == count { 0 } else { following };
    at
}

pub struct Sweep<'driver, 'turn, 'd, Report> {
    pub(crate) turn: schedule::Turn<'turn, 'd>,
    pub(crate) driver: &'driver mut driver::life::Lifecycle<'d, Report>,
    pub(crate) parks: usize,
    pub(crate) ahead: [usize; 2],
    pub(crate) groups: u32,
    pub(crate) coord: u32,
}