o3 0.61.0

shared-nothing primitives
Documentation
use std::{marker, mem};

use crate::depot::{
    self,
    arena::{linked, linked::slots},
    capacity,
};

/// Capacity retained by one input traversal, including its recycled slots.
pub struct Reserve<T, H: linked::Source<T>, Tag = ()> {
    pub(super) owner: H,
    pub(super) remaining: u32,
    domain: marker::PhantomData<fn(T, Tag) -> (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 slots::Store<T>, Tag> {
    /// Consumes one proved outstanding reservation without repeating admission.
    pub fn claim(
        proof: depot::raw::Proof<&mut Self, linked::Available>,
        value: T,
    ) -> slots::Live<T, &'a slots::Store<T>, Tag> {
        let reserve = proof.into_inner();
        reserve.remaining = reserve.remaining.wrapping_sub(1);
        unsafe { slots::Live::claim(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.into_raw_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, slots::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 { slots::Live::claim(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, &'static slots::Store<u64>>>() == 2 * mem::size_of::<usize>()
);

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