o3 0.60.0

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

use crate::{
    alloc,
    depot::{
        arena::{
            linked,
            linked::{fifo, head, slots},
        },
        capacity,
        index::Initialized,
        queue::pool::fifo::Fifo,
        raw::Proof,
    },
};

/// Fixed node arena for compact external list and FIFO owners.
pub struct Arena<T, Tag = (), const TRACK: bool = true> {
    pub(in crate::depot) inner: slots::Store<T>,
    domain: marker::PhantomData<fn(Tag) -> Tag>,
}

impl<T, Tag, const TRACK: bool> Arena<T, Tag, TRACK> {
    /// Exact requested node allocation layout.
    pub fn try_layout(capacity: usize) -> Result<Layout, alloc::Overflow> {
        Ok(slots::Capacity::<T>::new(capacity)?.bundle().layout())
    }

    pub fn try_new(capacity: usize) -> Result<Self, alloc::Error> {
        let capacity = slots::Capacity::new(capacity)?;
        let bundle = capacity.bundle();
        let nodes = bundle.try_allocate()?.root();
        Ok(Self {
            inner: unsafe { slots::Store::new(nodes, capacity) },
            domain: marker::PhantomData,
        })
    }

    pub fn value<'a, P>(live: Proof<(&'a Self, slots::Index<Tag>), Initialized<P>>) -> &'a T {
        let (arena, index) = live.into_inner();
        unsafe { arena.inner.value(index) }
    }
}

impl<T> Arena<T> {
    /// Borrows an independent FIFO from this exact region.
    pub const fn fifo(&self) -> Fifo<'_, T> {
        Fifo::new(self)
    }
}

impl<T, const TRACK: bool> Arena<T, (), TRACK> {
    /// # Safety
    /// The ends retain one unchanged live partition in this arena for the borrow.
    pub unsafe fn fifo_iter<'a>(
        &'a self,
        ends: &'a fifo::ends::Exclusive<slots::Index>,
    ) -> linked::Iter<'a, T> {
        unsafe { linked::Iter::new(&self.inner, ends.head_raw()) }
    }

    /// # Safety
    /// The ends retain one unchanged live partition in this arena for the borrow.
    pub unsafe fn fifo_front<'a>(
        &'a self,
        ends: &'a fifo::ends::Exclusive<slots::Index>,
    ) -> Option<(slots::Index, &'a T)> {
        let index = ends.head()?;
        Some((
            index,
            Self::value(unsafe { Proof::<_, Initialized<()>>::new((self, index)) }),
        ))
    }

    pub fn fifo_detach<'a>(
        proof: Proof<
            (
                (&'a Self, &mut fifo::ends::Exclusive<slots::Index>),
                slots::Index,
            ),
            fifo::Detach,
        >,
    ) -> slots::Live<T, slots::Borrow<'a, T, TRACK>> {
        let ((arena, ends), index) = proof.into_inner();
        unsafe { ends.unlink(&arena.inner, index) };
        unsafe { slots::Live::detached(slots::Borrow(&arena.inner), index) }
    }

    /// # Safety
    /// One unused node in this exact arena remains reserved through this call.
    /// When TRACK is false, its external credit outlives insertion or cancellation.
    pub unsafe fn reserve(&self) -> slots::Vacant<T, slots::Borrow<'_, T, TRACK>> {
        unsafe { slots::Vacant::reserve(slots::Borrow(&self.inner)) }
    }
}

impl<T, Tag> Arena<T, Tag> {
    pub fn available(&self) -> usize {
        self.inner.available()
    }

    pub fn try_reserve(
        &self,
        count: capacity::Count,
    ) -> Result<slots::Reserve<T, &slots::Store<T>, Tag>, capacity::Shortage> {
        slots::Reserve::try_new(&self.inner, count)
    }

    pub fn vacant(&self) -> Option<slots::Vacant<T, &'_ slots::Store<T>, Tag>> {
        self.inner.vacant()
    }

    pub fn permit(&mut self) -> Option<slots::Permit<'_, T, Tag>> {
        self.inner.permit()
    }

    /// # Safety
    /// `back` remains live in this exact node set.
    pub unsafe fn back_mut(&mut self, back: fifo::Back<Tag>) -> &mut T {
        unsafe { self.inner.back_mut(back) }
    }

    /// # Safety
    /// `state` owns one acyclic live partition in this exact node set.
    pub unsafe fn iter<'a>(&'a self, state: &'a head::List<Tag>) -> linked::Iter<'a, T, Tag> {
        unsafe { state.iter(&self.inner) }
    }

    /// # Safety
    /// `state` owns one acyclic live partition in this exact node set.
    pub unsafe fn cursor<'a>(
        &'a self,
        state: &'a mut head::List<Tag>,
    ) -> slots::Cursor<'a, T, Tag> {
        unsafe { state.cursor(&self.inner) }
    }

    /// # Safety
    /// `state` owns one acyclic live partition in this exact node set.
    pub unsafe fn front_entry<'a, 'b>(
        &'a self,
        state: &'b mut head::List<Tag>,
    ) -> Option<slots::Entry<'a, 'b, T, Tag>> {
        unsafe { state.front_entry(&self.inner) }
    }

    /// # Safety
    /// `state` owns one acyclic live partition in this exact node set.
    pub unsafe fn pop_front(&self, state: &mut head::List<Tag>) -> Option<T> {
        Some(unsafe { state.front_entry(&self.inner) }?.pop())
    }

    /// # Safety
    /// `state` owns one acyclic live partition in this exact node set.
    pub unsafe fn queue<'a>(&'a self, state: &'a mut fifo::Fifo<Tag>) -> slots::Bound<'a, T, Tag> {
        unsafe { state.bind(&self.inner) }
    }
}

impl<T, Tag, const TRACK: bool> Drop for Arena<T, Tag, TRACK> {
    fn drop(&mut self) {
        unsafe { self.inner.drop_values() };
        let bundle = self.inner.capacity().bundle();
        unsafe { bundle.deallocate(self.inner.pointer().cast()) };
    }
}

const _: [(); mem::size_of::<Arena<u64>>()] = [(); 3 * mem::size_of::<usize>()];

mod pool;
pub(in crate::depot) use pool::{PoolMutable, PoolPolicy};

mod queues;
pub(in crate::depot) use queues::{QueueBlock, QueueSlot};

mod slabs;
pub(in crate::depot) use slabs::{SlabCapacity, SlabLinks, SlabSlot};

mod root;
pub(in crate::depot) use root::RawSlot;

mod indexed;
pub(in crate::depot) use indexed::{IndexedBlock, IndexedHook, IndexedMeta, IndexedState};

mod staged;
pub(in crate::depot) use staged::{StagedNode, StagedPhase, StagedState};

mod ends;
pub use ends::EndsPop;