o3 0.62.0

shared-nothing primitives
Documentation
use std::{
    mem::{self, ManuallyDrop},
    ops::Deref,
    ptr,
};

use crate::depot::{
    arena::slab::{
        self, Plane, entries,
        key::{self, cycle, cycle::Policy},
    },
    queue::links,
};

pub(in crate::depot) struct Entry<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    S: slab::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> {
    pub(super) position: slab::raw::Position<'a, T, MAX, M, N, S, LIVE>,
}

impl<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    S: slab::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> Deref for Entry<'a, T, MAX, M, N, S, LIVE, ISSUED>
{
    type Target = slab::raw::Position<'a, T, MAX, M, N, S, LIVE>;

    fn deref(&self) -> &Self::Target {
        &self.position
    }
}

impl<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    S: slab::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> Entry<'a, T, MAX, M, N, S, LIVE, ISSUED>
{
    /// # Safety
    /// This position belongs to the original core and is exclusively held in the specified phase.
    pub(in crate::depot::arena::slab) unsafe fn new(
        core: &'a slab::raw::Core<T, MAX, M, N, S>,
        index: links::Index,
    ) -> Self {
        Self {
            position: unsafe { slab::raw::Position::new(core, index) },
        }
    }

    pub(in crate::depot::arena::slab) fn into_place(
        self,
    ) -> slab::raw::Position<'a, T, MAX, M, N, S, LIVE> {
        let held = ManuallyDrop::new(self);
        // SAFETY: the consuming operation transfers this one position and disarms the old Drop.
        unsafe { ptr::read(&held.position) }
    }

    fn phase<const NEXT_LIVE: bool, const NEXT_ISSUED: bool>(
        self,
    ) -> Entry<'a, T, MAX, M, N, S, NEXT_LIVE, NEXT_ISSUED> {
        let prior = self.into_place();
        // SAFETY: the owning transition established the destination phase before transfer.
        unsafe { Entry::new(prior.core(), prior.index()) }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const ISSUED: bool>
    Entry<'a, T, MAX, M, N, S, true, ISSUED>
{
    pub(in crate::depot) fn value(&self) -> &T {
        // SAFETY: the live phase retains the initialized value.
        unsafe { self.slot().value() }
    }

    pub(in crate::depot) fn value_mut(&mut self) -> &mut T {
        // SAFETY: the live phase retains the original exclusive slot loan.
        unsafe { &mut *self.slot().value_ptr() }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
    Entry<'a, T, MAX, M, N, S, true, false>
{
    pub(in crate::depot::arena::slab) fn withdraw(self) -> Entry<'a, T, MAX, M, N, S, false, true> {
        let member = slab::raw::Retirement::new(&self.position);
        // SAFETY: this live entry solely owns removal of this exact dense member.
        unsafe { member.unlink() };
        member.slot.metadata.state.set(slab::State::Reserved);
        self.phase()
    }

    pub(in crate::depot) fn remove(self) -> T {
        let member = slab::raw::Retirement::new(&self.into_place());
        // SAFETY: this live loan owns the value; detach transfers only physical retirement.
        let value = unsafe { member.slot.take_value() };
        // SAFETY: the value was transferred and no payload borrow survives.
        unsafe { member.recycle::<true>() };
        value
    }

    /// # Safety
    /// Destruction or transfer is complete and no value or pin loan remains.
    pub(in crate::depot::arena::slab) unsafe fn release(self) {
        unsafe { slab::raw::Retirement::new(&self.into_place()).recycle::<true>() };
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
    Entry<'a, T, MAX, M, N, S, true, true>
{
    pub(in crate::depot::arena::slab) fn commit(self) -> Entry<'a, T, MAX, M, N, S, true, false> {
        Entry {
            position: self.into_place(),
        }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const LIVE: bool>
    Entry<'a, T, MAX, M, N, S, LIVE, false>
{
    pub(in crate::depot) fn issue(self) -> Entry<'a, T, MAX, M, N, S, LIVE, true> {
        Entry {
            position: self.into_place(),
        }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
    Entry<'a, T, MAX, M, N, S, false, false>
{
    /// # Safety
    /// This exact reserved value is initialized and unborrowed.
    pub(in crate::depot::arena::slab) unsafe fn publish_key<Tag>(
        self,
    ) -> (
        key::Handle<Tag, MAX>,
        Entry<'a, T, MAX, M, N, S, true, false>,
    ) {
        let key = key::Handle::new(self.index().raw(), self.generation());
        (key, unsafe { self.publish() })
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const ISSUED: bool>
    Entry<'a, T, MAX, M, N, S, false, ISSUED>
{
    pub(in crate::depot) fn uninit(&mut self) -> &mut mem::MaybeUninit<T> {
        // SAFETY: this reservation owns the storage; the caller retains any transferred destruction.
        unsafe { &mut *self.slot().value_ptr().cast::<mem::MaybeUninit<T>>() }
    }

    pub(in crate::depot) fn install<const STAGED: bool>(
        mut self,
        value: T,
    ) -> Entry<'a, T, MAX, M, N, S, true, STAGED> {
        self.uninit().write(value);
        // SAFETY: this reservation now contains the initialized value without outstanding loans.
        unsafe { self.publish() }
    }

    /// # Safety
    /// The reserved value is complete and unborrowed.
    pub(in crate::depot::arena::slab) unsafe fn publish<const STAGED: bool>(
        self,
    ) -> Entry<'a, T, MAX, M, N, S, true, STAGED> {
        let pos = self.core().live.get();
        let slot = self.slot();
        // SAFETY: the retained reservation leaves room at the end of the live prefix.
        unsafe {
            self.core()
                .slots
                .tail()
                .get_unchecked(pos as usize)
                .set(mem::MaybeUninit::new(self.index().raw()))
        };
        self.core().live.set(pos + 1);
        slot.metadata.position.set(pos);
        slot.metadata.state.set(M::TAKEN);
        self.phase()
    }
}

impl<'a, T, const MAX: u32, const RECYCLE: bool, const N: usize, S: slab::Storage>
    Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, true, false>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub(in crate::depot) fn into_mut(self) -> &'a mut T {
        // SAFETY: this exclusive live loan transfers to T; its Drop needs no physical access.
        unsafe { &mut *self.slot().value_ptr() }
    }
}

impl<
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    S: slab::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> Drop for Entry<'_, T, MAX, M, N, S, LIVE, ISSUED>
{
    fn drop(&mut self) {
        if !ISSUED && LIVE {
            if M::REENTRANT {
                self.slot().metadata.state.set(slab::State::Occupied);
            }
            return;
        }
        let member = slab::raw::Retirement::new(&self.position);
        if ISSUED && LIVE {
            // SAFETY: this terminal phase owns the initialized value's sole destruction.
            unsafe { member.slot.drop_value() };
        }
        // SAFETY: the reservation is empty or its initialized value just ended.
        unsafe { member.recycle::<ISSUED>() };
    }
}

impl<'a, T, const MAX: u32, const N: usize, S: slab::Storage>
    Entry<'a, T, MAX, entries::Exclusive, N, S, true, false>
{
    pub(in crate::depot) fn replace(&mut self, next: key::Generation<MAX>, value: T) -> T {
        let prior = mem::replace(self.value_mut(), value);
        self.slot().metadata.generation.set(next);
        prior
    }
}