o3 0.59.0

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

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

/// Sole return entitlement, retaining the allocation's original owner.
pub type Vacant<T, H, Tag = ()> = Slot<T, H, Tag, false>;
/// An initialized detached slot and the owner that may publish it.
pub type Live<T, H, Tag = ()> = Slot<T, H, Tag, true>;

#[must_use = "a detached slot retains its sole return entitlement"]
pub struct Slot<T, H: linked::Source<T>, Tag, const LIVE: bool> {
    owner: H,
    index: slots::Index<Tag>,
    value: marker::PhantomData<T>,
}

impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Slot<T, H, Tag, LIVE> {
    /// # Safety
    /// The owner holds this exact unlinked slot and its return credit. Its payload is
    /// initialized exactly when LIVE, with no outstanding loans.
    pub(in crate::depot) unsafe fn detached(owner: H, index: slots::Index<Tag>) -> Self {
        Self {
            owner,
            index,
            value: marker::PhantomData,
        }
    }

    /// # Safety
    /// Transfer the slot, its initialization state and its sole return right to
    /// the original topology or reservation before exposing it to another caller.
    pub(crate) unsafe fn into_raw_parts(self) -> (H, slots::Index<Tag>) {
        let slot = mem::ManuallyDrop::new(self);
        (unsafe { ptr::read(&slot.owner) }, slot.index)
    }
}

impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Drop for Slot<T, H, Tag, LIVE> {
    fn drop(&mut self) {
        let store = self.owner.store();
        let index = self.index.raw();
        let node = unsafe { store.slot(index) };
        if LIVE {
            // SAFETY: this slot retains the value and its address throughout destruction.
            unsafe { (&mut *node.value.get()).assume_init_drop() };
        }
        store.recycle(index, node, H::TRACK);
    }
}

impl<T, H: linked::Source<T>, Tag> Vacant<T, H, Tag> {
    /// # 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(crate) 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::<false>() };
        unsafe { Self::detached(owner, slots::Index::new(index)) }
    }

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

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

impl<T, H: linked::Source<T>, Tag> Live<T, H, Tag> {
    /// # Safety
    /// The exact store has one debited slot ready for immediate initialization.
    pub(super) unsafe fn claim(owner: H, value: T) -> Self {
        let (index, node) = unsafe { owner.store().claim::<true>() };
        unsafe { node.value.get().cast::<T>().write(value) };
        unsafe { Self::detached(owner, slots::Index::new(index)) }
    }

    pub fn get(&self) -> &T {
        unsafe { self.owner.store().value(self.index) }
    }

    pub(crate) fn take(self) -> (T, Vacant<T, H, Tag>) {
        let (owner, index) = unsafe { self.into_raw_parts() };
        let node = unsafe { owner.store().slot(index.raw()) };
        let value = unsafe { (&*node.value.get()).assume_init_read() };
        node.next.set(index.raw());
        (value, unsafe { Vacant::detached(owner, index) })
    }

    /// # Safety
    /// The queue belongs to this exact store and the claim has no successor.
    pub(in crate::depot) unsafe fn append_to(self, queue: &mut fifo::Fifo<Tag>) {
        let (owner, index) = unsafe { self.into_raw_parts() };
        unsafe { queue.link::<_, false>(owner.store(), index) };
    }
}

impl<T, Tag> Live<T, &slots::Store<T>, Tag> {
    /// # Safety
    /// Transfer this initialized slot into a topology retaining the original store.
    pub(crate) unsafe fn into_index(self) -> slots::Index<Tag> {
        unsafe { self.into_raw_parts() }.1
    }

    /// # Safety
    /// The links are a live partition in this claim's exact store; the claim has no successor.
    pub(crate) unsafe fn append(self, links: &mut ends::Exclusive<slots::Index<Tag>>) {
        let (store, index) = unsafe { self.into_raw_parts() };
        unsafe { links.append_end(store, index) };
    }

    /// Publishes the initialized claim into the FIFO proved to retain its exact store.
    pub fn push_back(proof: depot::raw::Proof<(Self, &mut fifo::Fifo<Tag>), linked::Same>) {
        let (claim, queue) = proof.into_inner();
        unsafe { claim.append_to(queue) };
    }
}

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

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