o3 0.62.0

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

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

impl<T, H: linked::Source<T>, Tag> Live<T, H, Tag> {
    /// # Safety
    /// The owner retains this detached initialized 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
    /// The exact store has one debited slot ready for immediate initialization.
    pub(in crate::depot::arena::linked) unsafe fn claim<const BRANCH: u8>(
        owner: H,
        value: impl crate::mem::raw::Convert<T, BRANCH>,
    ) -> Self {
        let (index, slot) = unsafe { owner.store().claim(Some(linked::raw::Index::NONE)) };
        unsafe { (*slot).write(crate::mem::raw::Convert::<T, BRANCH>::into(value)) };
        unsafe { Self::detached(owner, index) }
    }

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

    pub(crate) fn take(self) -> (T, linked::raw::Vacant<T, H, Tag>) {
        let (owner, index) = unsafe { self.0.into_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 {
            linked::raw::Vacant::detached(owner, index)
        })
    }

    pub fn append_ends(
        proof: depot::raw::Proof<
            (Self, &mut ends::Exclusive<linked::raw::Index<Tag>>),
            fifo::Insert<false>,
        >,
    ) {
        let (node, ends) = proof.into_inner();
        let (owner, index) = unsafe { node.0.into_parts() };
        unsafe { ends.append_end(owner.store(), index) };
    }

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

impl<T, M: Copy, Tag, const TRACK: bool> Live<(T, M), linked::raw::Borrow<'_, (T, M), TRACK>, Tag> {
    /// Moves the payload to its final storage and returns this slot.
    pub fn take_into(self, place: &mut mem::MaybeUninit<T>) {
        let (linked::raw::Borrow(store), index) = unsafe { self.0.into_parts() };
        let node = unsafe { store.slot(index.raw()) };
        // SAFETY: this detached pair owns T; Copy metadata needs no destruction.
        unsafe {
            place
                .as_mut_ptr()
                .write(ptr::addr_of!((*node.value_ptr()).0).read());
        }
        store.recycle(index.raw(), node, TRACK);
    }
}

impl<T, Tag, const TRACK: bool> Live<T, linked::raw::Borrow<'_, T, TRACK>, Tag> {
    /// # Safety
    /// All owning fields have been consumed. No value loans or destruction or
    /// pin obligations remain.
    pub unsafe fn recycle(self) {
        let (linked::raw::Borrow(store), index) = unsafe { self.0.into_parts() };
        let index = index.raw();
        store.recycle(index, unsafe { store.slot(index) }, TRACK);
    }

    pub fn into_inner(self) -> T {
        let (linked::raw::Borrow(store), index) = unsafe { self.0.into_parts() };
        unsafe { store.take::<_, TRACK>(index) }
    }
}

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

    /// 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> Live<T, linked::raw::Bound<'_, T, Tag, TRACK>, Tag> {
    pub fn publish<const FRONT: bool>(self) {
        let (owner, index) = unsafe { self.0.into_parts() };
        // SAFETY: the bound owner retains this detached node's queue and store.
        unsafe { index.link::<_, FRONT>(owner.state, owner.store) };
    }
}