o3 0.62.0

shared-nothing primitives
Documentation
use crate::depot::{
    arena::slab::{
        self, Plane, entries,
        key::{self, cycle, cycle::Policy},
    },
    capacity,
};
use std::{marker, pin::Pin};

/// Slot storage whose initialized values remain pinned at their original addresses.
pub struct Pinned<
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    const RECYCLE: bool = false,
    const PARTITIONS: usize = 1,
    S = slab::Heap,
> where
    cycle::Mode<RECYCLE>: Policy,
    S: slab::Storage,
{
    pub(in crate::depot) core: slab::raw::Core<T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS, S>,
    _pin: marker::PhantomPinned,
    tag: marker::PhantomData<fn() -> Tag>,
}

impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
    cycle::Mode<RECYCLE>: Policy,
    S: slab::Storage,
    S::Plane<T, MAX, entries::Exclusive<RECYCLE>>:
        slab::HeapPlane<T, MAX, entries::Exclusive<RECYCLE>>,
{
    /// Allocates one pinned heap extent under its slot initialization policy.
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: slab::raw::Core::try_new(cap)?,
            _pin: marker::PhantomPinned,
            tag: marker::PhantomData,
        })
    }
}

impl<T, Tag, const MAX: u32, const PARTITIONS: usize, const N: usize> Default
    for Pinned<T, Tag, MAX, false, PARTITIONS, slab::Inline<N>>
{
    /// Builds the complete inline slot plane without allocating.
    fn default() -> Self {
        Self {
            core: slab::raw::Core::new(),
            _pin: marker::PhantomPinned,
            tag: marker::PhantomData,
        }
    }
}

impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
    cycle::Mode<RECYCLE>: Policy,
    S: slab::Storage,
{
    pub fn capacity(&self) -> usize {
        self.core.slots.capacity()
    }

    pub fn is_full(&self) -> bool {
        self.core.is_full()
    }

    pub fn vacant(
        self: Pin<&mut Self>,
    ) -> Option<slab::raw::Anchor<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
        let store = self.into_ref().get_ref();
        // SAFETY: the retained store came from this exclusive pinned loan.
        Some(unsafe { slab::raw::Anchor::pinned(store.core.free()?) })
    }

    pub fn anchor(
        self: Pin<&mut Self>,
        parts: key::Parts<MAX>,
    ) -> Option<slab::raw::Anchored<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
        let store = self.into_ref().get_ref();
        let entry = slab::raw::Position::key(&store.core, parts.index(), parts.generation())?;
        // SAFETY: this live slot is borrowed from the original pinned store.
        Some(unsafe { slab::raw::Anchored::pinned(entry.take()) })
    }
}