o3 0.62.0

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

use crate::{
    alloc,
    depot::{
        self,
        arena::linked::{self, fifo, fifo::ends, head},
        capacity,
        index::Initialized,
    },
};

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

impl<T, Tag, const TRACK: bool> Arena<T, Tag, TRACK> {
    pub fn try_new(capacity: usize) -> Result<Self, alloc::Error> {
        let capacity = linked::raw::Capacity::new(capacity)?;
        let bundle = capacity.bundle();
        let nodes = bundle.try_allocate()?.root();
        Ok(Self {
            inner: unsafe { capacity.bind(nodes) },
            domain: marker::PhantomData,
        })
    }

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

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 ends::Exclusive<linked::raw::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 ends::Exclusive<linked::raw::Index>,
    ) -> Option<(linked::raw::Index, &'a T)> {
        let index = ends.head()?;
        Some((
            index,
            Self::value(unsafe { depot::raw::Proof::<_, Initialized<()>>::new((self, index)) }),
        ))
    }

    pub fn fifo_detach<'a>(
        proof: depot::raw::Proof<
            (
                (&'a Self, &mut ends::Exclusive<linked::raw::Index>),
                linked::raw::Index,
            ),
            fifo::Detach,
        >,
    ) -> linked::raw::Live<T, linked::raw::Borrow<'a, T, TRACK>> {
        let ((arena, ends), index) = proof.into_inner();
        unsafe { ends.unlink(&arena.inner, index) };
        unsafe { linked::raw::Live::detached(linked::raw::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) -> linked::raw::Vacant<T, linked::raw::Borrow<'_, T, TRACK>> {
        unsafe { linked::raw::Vacant::reserve(linked::raw::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<linked::raw::Reserve<T, &linked::raw::Store<T>, Tag>, capacity::Shortage> {
        linked::raw::Reserve::try_new(&self.inner, count)
    }

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

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

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

    /// # 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>,
    ) -> linked::raw::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<linked::raw::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>,
    ) -> linked::raw::Bound<'a, T, Tag> {
        unsafe { linked::raw::Bound::new(state, &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>()];