o3 0.62.0

shared-nothing primitives
Documentation
use crate::depot::{arena::linked, capacity, queue::pool::Fifo};
use std::mem;

#[must_use]
#[repr(transparent)]
pub struct Vacant<T, H: linked::Source<T>, Tag = ()>(
    pub(super) linked::raw::Slot<T, H, Tag, false>,
);

impl<T, H: linked::Source<T>, Tag> Vacant<T, H, Tag> {
    /// # Safety
    /// The owner retains this detached uninitialized node and its sole return entitlement.
    pub(crate) unsafe fn detached(owner: H, index: linked::raw::Index<Tag>) -> Self {
        Self(unsafe { linked::raw::Slot::new(owner, index) })
    }

    /// # Safety
    /// This owner's store counts all available slots and the owner refunds each claim.
    pub(in crate::depot) unsafe fn try_reserve(owner: H) -> Option<Self> {
        if owner.store().available() == 0 {
            return None;
        }
        Some(unsafe { Self::reserve(owner) })
    }

    fn debit(owner: &H) {
        if H::TRACK {
            owner.store().debit(capacity::Count::new(1));
        }
    }

    /// # Safety
    /// This exact owner has one available slot.
    pub unsafe fn reserve(owner: H) -> Self {
        Self::debit(&owner);
        unsafe { Self::claim(owner) }
    }

    /// # Safety
    /// One already debited reservation is consumed from this exact store.
    pub(in crate::depot::arena::linked) unsafe fn claim(owner: H) -> Self {
        let (index, _) = unsafe { owner.store().claim(None) };
        unsafe { Self::detached(owner, index) }
    }

    /// # Safety
    /// This exact owner has one available slot.
    pub(crate) unsafe fn insert_one<const BRANCH: u8>(
        owner: H,
        value: impl crate::mem::raw::Convert<T, BRANCH>,
    ) -> linked::raw::Live<T, H, Tag> {
        Self::debit(&owner);
        unsafe { linked::raw::Live::claim::<BRANCH>(owner, value) }
    }

    /// # Safety
    /// This exact owner has one available slot, disjoint from `value`'s byte extent.
    pub(crate) unsafe fn copy_one(owner: H, value: &T) -> linked::raw::Live<T, H, Tag>
    where
        T: Copy,
    {
        Self::debit(&owner);
        let (index, slot) = unsafe { owner.store().claim(Some(linked::raw::Index::NONE)) };
        unsafe { slot.cast::<T>().copy_from_nonoverlapping(value, 1) };
        unsafe { linked::raw::Live::detached(owner, index) }
    }

    pub fn get_mut(&mut self) -> &mut mem::MaybeUninit<T> {
        let node = unsafe { self.0.owner.store().slot(self.0.index.raw()) };
        // SAFETY: this vacancy solely owns the exact uninitialized field.
        unsafe { &mut *node.value.get() }
    }

    /// # Safety
    /// This exact field contains an initialized T and retains no value loans.
    pub unsafe fn assume_init(self) -> linked::raw::Live<T, H, Tag> {
        let (owner, index) = unsafe { self.0.into_parts() };
        let node = unsafe { owner.store().slot(index.raw()) };
        node.next.set(linked::raw::Store::<T>::END);
        unsafe { linked::raw::Live::detached(owner, index) }
    }

    pub fn insert(self, value: T) -> linked::raw::Live<T, H, Tag> {
        let (owner, index) = unsafe { self.0.into_parts() };
        let node = unsafe { owner.store().slot(index.raw()) };
        unsafe { node.value.get().cast::<T>().write(value) };
        node.next.set(linked::raw::Store::<T>::END);
        unsafe { linked::raw::Live::detached(owner, index) }
    }
}

impl<T, Tag, const TRACK: bool> crate::mem::Return
    for Vacant<T, linked::raw::Bound<'_, T, Tag, TRACK>, Tag>
{
    type Item = T;

    fn return_item(self, item: T) {
        self.insert(item).publish::<true>();
    }
}

impl<'a, T, Tag> Vacant<T, &'a linked::raw::Store<T>, Tag> {
    pub(crate) fn try_new(store: &'a linked::raw::Store<T>) -> Option<Self> {
        unsafe { Self::try_reserve(store) }
    }
}

impl<'a, T, Tag, const TRACK: bool> Vacant<T, linked::raw::Bound<'a, T, Tag, TRACK>, Tag> {
    pub fn append(self, value: T) {
        self.insert(value).publish::<false>();
    }
}

impl<'fifo, 'pool, T> Vacant<T, &'fifo mut Fifo<'pool, T>> {
    /// Initializes and publishes this reserved node into its original FIFO.
    pub fn push_back(self, value: T) {
        let (fifo, index) = unsafe { self.insert(value).0.into_parts() };
        unsafe { fifo.links.append_end(&fifo.arena.inner, index) };
    }
}