o3 0.62.0

shared-nothing primitives
Documentation
pub use vacancy::Vacancy;
mod vacancy;
use runtime::entry;
use std::marker;

use crate::base::num::bounded;
use crate::depot::{
    arena::{
        hash::{self, runtime},
        slab::key,
    },
    map,
    queue::links,
};

/// An initialized resident entry carrying its directory membership phase.
pub struct Live<'a, T, Tag = (), P = entry::Occupied> {
    pub(in crate::depot::arena::hash) core: &'a mut hash::raw::Core<T, Tag>,
    pub(in crate::depot::arena::hash) index: bounded::raw::U32<0, { u32::MAX - 1 }>,
    pub(in crate::depot::arena::hash) phase: marker::PhantomData<fn(P) -> P>,
}

pub enum Phase<'a, T, Tag = ()> {
    Indexed(Live<'a, T, Tag, Indexed>),
    Staged(Live<'a, T, Tag, Staged>),
}

impl<'a, T, Tag> Live<'a, T, Tag, ()> {
    pub fn into_phase(self) -> Phase<'a, T, Tag> {
        hash::raw::phase(self)
    }

    pub fn unindex(self) -> Live<'a, T, Tag, Staged> {
        match self.into_phase() {
            Phase::Indexed(entry) => entry.unindex(),
            Phase::Staged(entry) => entry,
        }
    }

    pub fn discard(self) {
        hash::raw::discard(self.unindex());
    }
}

/// An indexed slot with removal-only directory authority.
pub struct Occupied;
/// An indexed slot whose owner permits a retained unindexed value.
pub struct Indexed;
/// An initialized slot absent from its resident directory.
pub struct Staged;

pub(in crate::depot::arena::hash) type Reservation<E, Tag> =
    Result<map::Vacancy<E, u64>, (E, key::Handle<runtime::Domain<Tag>>)>;

impl<'a, T, Tag> entry::Live<'a, T, Tag, Occupied> {
    /// Points to this live row without borrowing any field of the value.
    pub fn as_ptr(&self) -> *mut T {
        hash::raw::selected(self)
    }

    pub fn remove(self) -> T {
        hash::raw::remove(self)
    }

    pub fn discard(self) {
        hash::raw::erase(self);
    }
}

impl<'a, T, Tag> entry::Live<'a, T, Tag, Indexed> {
    pub fn unindex(self) -> Live<'a, T, Tag, Staged> {
        hash::raw::unindex(self)
    }

    pub(in crate::depot::arena::hash) fn occupied(self) -> Live<'a, T, Tag, Occupied> {
        Live {
            core: self.core,
            index: self.index,
            phase: marker::PhantomData,
        }
    }
}

const _: () = {
    assert!(size_of::<Live<'static, u64, (), ()>>() == 2 * size_of::<usize>());
    assert!(size_of::<entry::Live<'static, u64, (), Occupied>>() == 2 * size_of::<usize>());
    assert!(
        size_of::<Option<entry::Live<'static, u64, (), Occupied>>>()
            == size_of::<entry::Live<'static, u64, (), Occupied>>()
    );
    assert!(size_of::<Live<'static, u64, (), Staged>>() == 2 * size_of::<usize>());
};

impl<'a, T, Tag> Live<'a, T, Tag, Staged> {
    pub fn try_reserve<Q: Eq + ?Sized>(self, hash: u64, key: &Q) -> Reservation<Self, Tag>
    where
        T: AsRef<Q>,
    {
        hash::raw::reserve(self, hash, key)
    }
}

impl<'a, T, Tag> map::Vacancy<Live<'a, T, Tag, Staged>, u64> {
    pub fn get(&self) -> &T {
        self.source().get()
    }

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

    pub fn commit(self) -> Live<'a, T, Tag, Indexed> {
        hash::raw::commit(self)
    }
}

impl<'a, T, Tag, P> Live<'a, T, Tag, P> {
    pub fn slot_index(&self) -> links::Index {
        self.index.into()
    }

    pub fn key(&self) -> key::Handle<runtime::Domain<Tag>> {
        hash::raw::key(self)
    }

    pub fn get(&self) -> &T {
        hash::raw::value(self)
    }

    pub fn get_mut(&mut self) -> &mut T {
        hash::raw::value_mut(self)
    }

    pub fn into_mut(self) -> &'a mut T {
        hash::raw::into_value(self)
    }
}