o3 0.61.0

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

use cycle::Policy;

use crate::depot::{
    arena::slab::{
        self, entries,
        key::{self, cycle},
        lookup,
        storage::Plane as _,
    },
    capacity::Physical,
    index::Initialized,
    queue::links,
};

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub fn value_ptr<P>(
        live: crate::depot::raw::Proof<(&Self, links::Index), Initialized<P>>,
    ) -> *mut T {
        let (store, index) = live.into_inner();
        unsafe {
            store
                .core
                .slots
                .as_ref()
                .get_unchecked(index.get())
                .value_ptr()
        }
    }

    pub fn discard<P>(
        live: crate::depot::raw::Proof<(&Self, links::Index), crate::depot::index::Unborrowed<P>>,
    ) {
        let (store, index) = live.into_inner();
        let live = unsafe {
            crate::depot::raw::Proof::<_, Initialized<P>>::new(((&store.core, ()), index))
        };
        let (slot, ()): (
            entries::Busy<'_, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS, P>,
            (),
        ) = live.into();
        slot.discard();
    }

    /// Recovers the exact current generation-bearing key for one occupied
    /// direct-address slot.
    pub fn key_at(&'a self, index: usize) -> Option<key::Handle<Tag, MAX>> {
        if index > u32::MAX as usize {
            return None;
        }
        let index = index as u32;
        let entry = entries::Occupied::at(&self.core, index)?;
        Some(key::Handle::new(index, entry.generation()))
    }

    /// Borrows the current value at a physical slot through the owner's shared loan.
    /// Checks occupancy without matching a generation.
    pub fn value_at(&'a self, index: u32) -> Option<&'a T> {
        Some(entries::Occupied::at(&self.core, index)?.read())
    }
}

impl<'a, T, Tag, const MAX: u32> slab::Store<T, Tag, MAX> {
    /// Borrows each usable physical slot once, retaining disjoint entry proofs.
    pub fn entries(&'a mut self) -> impl Iterator<Item = slab::Entry<'a, T, Tag, MAX>> {
        let core = &self.core;
        core.slots
            .as_ref()
            .iter()
            .enumerate()
            .filter_map(move |(index, slot)| {
                match slot.metadata.state.get() {
                    slab::State::Occupied => {
                        Some(slab::Entry::Occupied(slab::Occupied::new(entries::Busy {
                            core,
                            slot,
                            plane: marker::PhantomData,
                        })))
                    }
                    slab::State::Free => {
                        // SAFETY: this unique visit proved the original slot's free membership.
                        let free = unsafe { entries::Free::new(core, slot, index as u32) };
                        Some(slab::Entry::Vacant(slab::Vacant::new(free.take())))
                    }
                    slab::State::Reserved | slab::State::Retired(()) => None,
                }
            })
    }
}

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub fn vacant_at(
        &'a mut self,
        index: u32,
    ) -> Option<
        slab::Vacant<entries::Pending<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
    > {
        let pending = self.core.at(index)?;
        Some(slab::Vacant::new(pending))
    }

    /// Reserves the first reusable slot in `range` without leaving it.
    /// Retired, occupied, and reserved slots are skipped.
    pub fn vacant_in(
        &'a mut self,
        range: ops::Range<u32>,
    ) -> Option<
        slab::Vacant<entries::Pending<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
    > {
        let pending = self.core.in_range(range)?;
        Some(slab::Vacant::new(pending))
    }

    pub fn entry_at(
        &'a mut self,
        index: u32,
    ) -> Option<
        slab::Occupied<entries::Busy<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
    > {
        let entry = entries::Occupied::at(&self.core, index)?;
        Some(slab::Occupied::new(entry.take()))
    }

    pub fn try_entry_at(
        &'a mut self,
        index: u32,
    ) -> Occupied<'a, T, Tag, MAX, RECYCLE, PARTITIONS> {
        let entry = entries::Occupied::try_at(&self.core, index)?;
        Ok(slab::Occupied::new(entry.take()))
    }
}

impl<'a, T, Tag, const MAX: u32> slab::Store<T, Tag, MAX> {
    pub(in crate::depot) fn contains(proof: PhysicalSlot<'a, T, Tag, MAX>) -> bool {
        let (source, key) = proof.into_inner();
        let slots = source.core.slots.as_ref();
        // SAFETY: the retained bound selects this eager owner's initialized extent.
        let slot = unsafe { slots.get_unchecked(key.index() as usize) };
        slot.metadata.state.get() == slab::State::Occupied
            && slot.metadata.generation.get() == key.generation()
    }
}

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool> slab::Cell<T, Tag, MAX, RECYCLE>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub(in crate::depot::arena::slab) fn vacancy_at(
        &'a self,
        index: u32,
    ) -> Option<slab::Vacant<entries::Pending<'a, T, MAX, entries::Interior<RECYCLE>>, Tag>> {
        let pending = self.core.at(index)?;
        Some(slab::Vacant::new(pending))
    }

    pub(in crate::depot::arena::slab) fn reserve(
        live: crate::depot::raw::Proof<
            (&'a slab::Cell<T, Tag, MAX, RECYCLE>, u32),
            Initialized<entries::Allocated>,
        >,
    ) -> Option<slab::Vacant<entries::Pending<'a, T, MAX, entries::Interior<RECYCLE>>, Tag>> {
        // SAFETY: this maps the retained Cell directly to its original core.
        let live = unsafe { live.map(|(owner, index)| (&owner.core, index)) };
        let pending = entries::Core::allocated(live)?;
        Some(slab::Vacant::new(pending))
    }

    pub(in crate::depot::arena::slab) fn current(
        live: crate::depot::raw::Proof<
            (&'a slab::Cell<T, Tag, MAX, RECYCLE>, u32),
            Initialized<entries::Allocated>,
        >,
    ) -> Borrowed<'a, T, Tag, MAX, RECYCLE> {
        // SAFETY: this maps the retained Cell directly to its original core.
        let live = unsafe { live.map(|(owner, index)| (&owner.core, index)) };
        Ok(slab::Borrowed::new(
            entries::Core::try_occupied(live)?.take(),
        ))
    }

    pub fn entry_at(
        &'a self,
        index: u32,
    ) -> Option<slab::Borrowed<entries::Busy<'a, T, MAX, entries::Interior<RECYCLE>>, Tag>> {
        let entry = entries::Occupied::at(&self.core, index)?;
        Some(slab::Borrowed::new(entry.take()))
    }

    pub(in crate::depot::arena::slab) fn try_entry_at(
        &'a self,
        index: u32,
    ) -> Borrowed<'a, T, Tag, MAX, RECYCLE> {
        Ok(slab::Borrowed::new(
            entries::Occupied::try_at(&self.core, index)?.take(),
        ))
    }
}

type Occupied<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize> =
    lookup::Result<
        slab::Occupied<entries::Busy<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
        entries::Exclusive<RECYCLE>,
    >;

type PhysicalSlot<'a, T, Tag, const MAX: u32> =
    crate::depot::raw::Proof<(&'a slab::Store<T, Tag, MAX>, key::Handle<Tag, MAX>), Physical>;

type Borrowed<'a, T, Tag, const MAX: u32, const RECYCLE: bool> = lookup::Result<
    slab::Borrowed<entries::Busy<'a, T, MAX, entries::Interior<RECYCLE>>, Tag>,
    entries::Interior<RECYCLE>,
>;