o3 0.60.0

shared-nothing primitives
Documentation
use std::pin::Pin;

use crate::depot::{
    arena::slab::{self, entries, external, storage},
    capacity,
};

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()
    }

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