o3 0.62.0

shared-nothing primitives
Documentation
use crate::depot::{
    self,
    arena::linked::{self, fifo::ends},
    capacity,
};
use std::{marker, mem};

/// Capacity retained by one input traversal, including its recycled slots.
pub struct Reserve<T, H: linked::Source<T>, Tag = ()> {
    pub(in crate::depot::arena::linked) owner: H,
    pub(in crate::depot::arena::linked) remaining: u32,
    domain: depot::raw::Domain<T, Tag>,
}

impl<T, H: linked::Source<T>, Tag> Reserve<T, H, Tag> {
    pub(crate) fn try_new(owner: H, count: capacity::Count) -> Result<Self, capacity::Shortage> {
        owner.store().check(count)?;
        // SAFETY: this store just admitted the complete count.
        Ok(unsafe { Self::new(owner, count) })
    }

    /// # Safety
    /// This exact store currently has at least `count` available slots.
    pub(crate) unsafe fn new(owner: H, count: capacity::Count) -> Self {
        owner.store().debit(count);
        Self {
            owner,
            remaining: count.raw(),
            domain: marker::PhantomData,
        }
    }
}

impl<'a, T, Tag> Reserve<T, &'a linked::raw::Store<T>, Tag> {
    /// Consumes one proved outstanding reservation without repeating admission.
    pub fn claim(
        proof: depot::raw::Proof<&mut Self, linked::Available>,
        value: T,
    ) -> linked::raw::Live<T, &'a linked::raw::Store<T>, Tag> {
        let reserve = proof.into_inner();
        reserve.remaining = reserve.remaining.wrapping_sub(1);
        unsafe { linked::raw::Live::claim::<0>(reserve.owner, value) }
    }

    /// Returns this exact store's vacant slot to the traversal's reserved capacity.
    pub fn recycle(proof: Vacancy<'_, 'a, Self, T, Tag>) {
        let (reserve, slot) = proof.into_inner();
        // SAFETY: this proof transfers the slot back to this traversal's reserved count.
        let (store, index) = unsafe { slot.0.into_parts() };
        let index = index.raw();
        store.recycle(index, unsafe { store.slot(index) }, false);
        reserve.remaining = reserve.remaining.wrapping_add(1);
    }
}

impl<T, Tag> Reserve<T, linked::raw::Bound<'_, T, Tag>, Tag> {
    pub fn append(
        proof: depot::raw::Proof<&mut mem::ManuallyDrop<Self>, linked::Available>,
        value: T,
    ) {
        let owner = &mut proof.into_inner().owner;
        // SAFETY: this unchanged source item consumes the exact bound queue debit.
        unsafe { linked::raw::Live::claim::<0>(owner.store, value).append_to(owner.state) };
    }
}

impl<T, H: linked::Source<T>, Tag> Drop for Reserve<T, H, Tag> {
    fn drop(&mut self) {
        self.owner.store().refund(self.remaining);
    }
}

const _: () = assert!(
    mem::size_of::<Reserve<u64, &'_ linked::raw::Store<u64>>>() == 2 * mem::size_of::<usize>()
);

type Vacancy<'s, 'a, S, T, Tag> = depot::raw::Proof<
    (
        &'s mut S,
        linked::raw::Vacant<T, &'a linked::raw::Store<T>, Tag>,
    ),
    linked::Same,
>;

impl<T, Tag> Reserve<T, &linked::raw::Store<T>, Tag> {
    /// # Safety
    /// The distinct source solely owns this nonempty chain of the reserved length.
    /// Conversion returns normally without observing or changing either store.
    pub(crate) unsafe fn move_from(
        self,
        source: &linked::raw::Store<impl Into<T>>,
        links: ends::Exclusive<linked::raw::Index<Tag>>,
    ) -> ends::Exclusive<linked::raw::Index<Tag>> {
        let mut output = ends::Exclusive::empty();
        // SAFETY: the entire input is transferred before the empty destination.
        unsafe {
            self.move_through::<false>(source, links.head_raw(), links.tail_raw(), &mut output)
        };
        output
    }

    /// # Safety
    /// The distinct source solely owns the reserved run and retains its suffix. The destination has a tail when NONEMPTY.
    /// Conversion returns normally without observing or changing either store.
    pub(crate) unsafe fn move_through<const NONEMPTY: bool>(
        self,
        source: &linked::raw::Store<impl Into<T>>,
        head: linked::raw::Index<Tag>,
        stop: linked::raw::Index<Tag>,
        suffix: &mut ends::Exclusive<linked::raw::Index<Tag>>,
    ) {
        let reserve = mem::ManuallyDrop::new(self);
        let store = reserve.owner;
        let after = suffix.head_raw();
        // SAFETY: the reservation covers the run and this is the store's current fresh frontier.
        let (first, last) = unsafe {
            store.move_run::<false, _>(
                source,
                (head, stop),
                after.raw(),
                reserve.remaining,
                store.initialized.get(),
            )
        };
        let tail = if !NONEMPTY && after == linked::raw::Index::NONE {
            last
        } else {
            suffix.tail_raw()
        };
        // SAFETY: these ends now cover the transferred run and its original suffix.
        *suffix = unsafe { ends::Exclusive::from_ends(first, tail) };
    }
}