o3 0.62.0

shared-nothing primitives
Documentation
use crate::depot::{
    arena::slab::{self, entries, key, lookup},
    capacity::Physical,
    index::Initialized,
    queue::links,
};
use crate::mem::Rejected;
use std::{convert, marker};

/// One initialized slot retaining its exclusive value loan.
pub struct Occupied<
    'a,
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    M: entries::Mode = entries::Exclusive,
    const N: usize = 1,
    const ROOT: bool = true,
    const OWNED: bool = false,
> {
    pub(in crate::depot) entry: slab::raw::Entry<'a, T, MAX, M, N, slab::Heap, true, OWNED>,
    tag: marker::PhantomData<fn() -> Tag>,
}

impl<
    'a,
    T,
    Tag,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    const ROOT: bool,
    const OWNED: bool,
> Occupied<'a, T, Tag, MAX, M, N, ROOT, OWNED>
{
    pub(in crate::depot::arena::slab) fn new(
        entry: slab::raw::Entry<'a, T, MAX, M, N, slab::Heap, true, OWNED>,
    ) -> Self {
        Self {
            entry,
            tag: marker::PhantomData,
        }
    }

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

    pub fn get(&self) -> &T {
        self.entry.value()
    }

    pub fn get_mut(&mut self) -> &mut T {
        self.entry.value_mut()
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, const ROOT: bool>
    Occupied<'a, T, Tag, MAX, entries::Exclusive, N, ROOT>
{
    pub fn into_mut(self) -> &'a mut T {
        self.entry.into_mut()
    }

    /// # Safety
    /// T's ownership and destruction obligations have transferred or completed.
    /// No value loan or pinning obligation remains.
    pub unsafe fn release(self) {
        // SAFETY: the consumed value leaves only this issued identity to retire.
        unsafe { self.entry.release() };
    }

    pub fn take(
        self,
    ) -> (
        slab::raw::Vacant<'a, T, Tag, MAX, entries::Exclusive, N, ROOT, true>,
        T,
    ) {
        // SAFETY: this exclusive live loan owns the value's sole take authority.
        let value = unsafe { self.entry.slot().take_value() };
        (slab::raw::Vacant::new(self.entry.withdraw()), value)
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize> Occupied<'a, T, Tag, MAX, entries::Exclusive, N> {
    pub fn try_new(
        owner: &'a mut slab::Store<T, Tag, MAX, N>,
        key: key::Handle<Tag, MAX>,
    ) -> Result<Self, Rejected<key::Handle<Tag, MAX>, key::Observation<MAX, convert::Infallible, ()>>>
    {
        Ok(Self::new(
            slab::raw::Position::try_key(&owner.core, key)?.take(),
        ))
    }

    pub fn at(owner: &'a mut slab::Store<T, Tag, MAX, N>, index: u32) -> Option<Self> {
        Some(Self::new(
            slab::raw::Position::at(&owner.core, index)?.take(),
        ))
    }

    pub fn try_at(
        owner: &'a mut slab::Store<T, Tag, MAX, N>,
        index: u32,
    ) -> Result<Self, lookup::Error<convert::Infallible, ()>> {
        Ok(Self::new(
            slab::raw::Position::try_at(&owner.core, index)?.take(),
        ))
    }

    pub fn from_live<P>(
        live: crate::depot::raw::Proof<
            (&'a mut slab::Store<T, Tag, MAX, N>, links::Index),
            Initialized<P>,
        >,
    ) -> Self {
        let (owner, index) = live.into_inner();
        // SAFETY: the original proof retains this exclusive initialized slot.
        Self::new(unsafe { owner.core.project(index) })
    }

    pub fn project<P>(live: crate::depot::raw::Proof<(&Self, links::Index), Initialized<P>>) -> &T {
        let (owner, index) = live.into_inner();
        // SAFETY: this initialized neighbor stays in the same exclusive root loan.
        unsafe { owner.entry.core().read(index) }
    }
}

impl<'a, T, Tag, const MAX: u32>
    TryFrom<
        crate::depot::raw::Proof<
            (&'a mut slab::Store<T, Tag, MAX>, key::Handle<Tag, MAX>),
            Physical,
        >,
    > for Occupied<'a, T, Tag, MAX>
{
    type Error = (
        key::Handle<Tag, MAX>,
        key::Observation<MAX, convert::Infallible, (), convert::Infallible>,
    );

    fn try_from(
        proof: crate::depot::raw::Proof<
            (&'a mut slab::Store<T, Tag, MAX>, key::Handle<Tag, MAX>),
            Physical,
        >,
    ) -> Result<Self, Self::Error> {
        let (owner, key) = proof.into_inner();
        // SAFETY: the original physical proof bounds this key in the same owner.
        Ok(Self::new(
            unsafe { slab::raw::Position::try_bound(&owner.core, key) }?.take(),
        ))
    }
}

impl<T, Tag, const MAX: u32, M: entries::Mode, const N: usize, const ROOT: bool>
    Occupied<'_, T, Tag, MAX, M, N, ROOT>
{
    pub fn remove(self) -> T {
        self.entry.remove()
    }

    pub fn discard(self) {
        self.entry.issue();
    }
}

impl<T, Tag, const MAX: u32, M: entries::Mode, const N: usize, const ROOT: bool>
    Occupied<'_, T, Tag, MAX, M, N, ROOT, true>
{
    pub fn publish(self) {
        self.entry.commit();
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, const ROOT: bool>
    Occupied<'a, T, Tag, MAX, entries::Exclusive, N, ROOT, true>
{
    pub fn commit(self) -> Occupied<'a, T, Tag, MAX, entries::Exclusive, N, ROOT> {
        Occupied::new(self.entry.commit())
    }
}