o3 0.61.0

shared-nothing primitives
Documentation
use cycle::Policy;

use crate::depot::arena::slab::{
    self, entries, entry,
    key::{self, cycle},
    storage,
};

/// One reserved slot of a pinned store awaiting its first value.
///
/// The pinned owner forwards its own lifetime and tag; the free list and the
/// generation check stay in o3 so both storage policies share them.
pub type Anchor<
    'a,
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    const RECYCLE: bool = false,
    const PARTITIONS: usize = 1,
    S = storage::Heap,
> = entry::Slot<
    entries::Pending<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS, (), S>,
    Tag,
    true,
    entry::Pinned,
>;

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    Anchor<'a, T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
    cycle::Mode<RECYCLE>: Policy,
    S: storage::Storage,
{
    pub fn index(&self) -> u32 {
        self.entry.index().raw()
    }

    fn key(&self) -> key::Handle<Tag, MAX> {
        key::Handle::new(self.index(), self.entry.generation())
    }

    /// Installs the first value and returns its identity.
    pub fn insert(self, value: T) -> key::Handle<Tag, MAX> {
        self.occupy(value).0
    }

    /// Installs the first value and projects it at its final address.
    pub fn occupy(
        mut self,
        value: T,
    ) -> (
        key::Handle<Tag, MAX>,
        slab::Anchored<'a, T, Tag, MAX, RECYCLE, PARTITIONS, S>,
    ) {
        self.entry.uninit().write(value);
        // SAFETY: the exact reserved value was initialized above.
        unsafe { self.assume_init() }
    }

    /// # Safety
    /// The exact reserved value is initialized, unborrowed, and was not previously pinned.
    pub(in crate::depot) unsafe fn assume_init(
        self,
    ) -> (
        key::Handle<Tag, MAX>,
        slab::Anchored<'a, T, Tag, MAX, RECYCLE, PARTITIONS, S>,
    ) {
        let key = self.key();
        // SAFETY: this consumes the same initialized reservation.
        let busy = unsafe { self.entry.assume_init() };
        // The original pinned owner and selected slot continue through polling and retirement.
        (key, entry::Slot::from_parts(busy, self.state))
    }
}