o3 0.61.0

shared-nothing primitives
Documentation
use std::{pin::Pin, ptr};

use crate::depot::{
    arena::slab::{self, entries, external, storage},
    capacity,
    index::Initialized,
    queue::links,
    raw::Proof,
};

pub(in crate::depot) type Inner<T, G, Tag> =
    slab::Pinned<external::Stamped<T, G>, Tag, 1, true, 1, storage::Lazy>;

pub struct Pinned<T, G: external::Generation, Tag = ()> {
    pub(in crate::depot) inner: Inner<T, G, Tag>,
    pub(in crate::depot) next: Option<G>,
}

pub type Anchor<'a, T, G, Tag = ()> = external::grant::Reservation<
    'a,
    slab::Anchor<'a, external::Stamped<T, G>, Tag, 1, true, 1, storage::Lazy>,
    G,
>;

pub struct Anchored<'a, T, G: external::Generation, Tag = ()>(
    pub(in crate::depot) slab::Anchored<'a, external::Stamped<T, G>, Tag, 1, true, 1, storage::Lazy>,
);

impl<T, G: external::Generation, Tag> Pinned<T, G, Tag> {
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            inner: Inner::try_new(cap)?,
            next: Some(G::INITIAL),
        })
    }

    pub fn capacity(&self) -> capacity::Count {
        storage::Plane::count(&self.inner.core.slots)
    }

    pub fn try_grant(self: Pin<&mut Self>) -> Result<Anchor<'_, T, G, Tag>, slab::Vacancy> {
        let (next, inner) = self.split();
        external::grant::Reservation::try_new(next, || inner.vacant())
    }

    pub fn try_occupied(
        self: Pin<&mut Self>,
        key: external::Key<G, Tag>,
    ) -> Result<Anchored<'_, T, G, Tag>, external::errors::Lookup<G, Tag>> {
        let owner = self.into_ref().get_ref();
        let entry = entries::Occupied::try_at(&owner.inner.core, key.index())
            .map_err(|cause| external::errors::Lookup::Physical { key, cause })?
            .take();
        key.check(entry.value().generation)?;
        // SAFETY: the validated stamped value retains the original exclusive pinned loan.
        Ok(Anchored(unsafe { slab::Anchored::pinned(entry) }))
    }
}

impl<'a, T, G: external::Generation, Tag> Anchored<'a, T, G, Tag> {
    pub fn value(&mut self) -> Pin<&mut T> {
        self.0.value().pinned()
    }

    pub fn into_value(self) -> Pin<&'a mut T> {
        self.0.into_value().pinned()
    }

    /// Returns the pinned value's storage without creating a value reference.
    /// Raw access must preserve pinning and this guard's exclusive value loan.
    pub fn as_mut_ptr(&mut self) -> *mut T {
        // SAFETY: the original guard owns this stamped slot's value storage.
        unsafe { ptr::addr_of_mut!((*self.0.entry.slot.value_ptr()).value) }
    }

    /// Borrows an initialized neighbor through the original pinned storage loan.
    pub fn project<Q>(live: Proof<(&Self, links::Index), Initialized<Q>>) -> &T {
        let (owner, index) = live.into_inner();
        // SAFETY: the unchanged proof names an initialized row in this exact core.
        let live = unsafe { Proof::<_, Initialized<Q>>::new((&owner.0.entry, index)) };
        &entries::Busy::project(live).1.value
    }

    /// # Safety
    /// T's destruction is complete and no value borrow or pin obligation remains.
    pub unsafe fn discard_uninit(self) {
        // SAFETY: T is destroyed and the remaining generation is Copy.
        unsafe { self.0.discard_uninit() };
    }

    pub fn discard(self) {
        self.0.discard();
    }
}