o3 0.62.0

shared-nothing primitives
Documentation
use std::marker;

use crate::depot::{
    arena::{
        hash::{
            self, errors,
            runtime::{self, entry},
        },
        slab::key,
    },
    map,
};

#[repr(transparent)]
pub struct Map<T, Tag = (), P = entry::Occupied> {
    pub(in crate::depot::arena::hash) core: hash::raw::Core<T, Tag>,
    phase: marker::PhantomData<fn(P) -> P>,
}

impl<T, Tag, P> Map<T, Tag, P> {
    pub fn try_new(plan: hash::Plan) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: hash::raw::Core::try_new(plan)?,
            phase: marker::PhantomData,
        })
    }
    pub const fn capacity(&self) -> usize {
        self.core.capacity()
    }
    pub const fn is_empty(&self) -> bool {
        self.core.len == 0
    }
    pub fn get(&self, key: key::Handle<runtime::Domain<Tag>>) -> Option<&T> {
        hash::raw::get(&self.core, key)
    }
    pub fn key_value_at(&self, index: usize) -> Option<(key::Handle<runtime::Domain<Tag>>, &T)> {
        hash::raw::pair(&self.core, index)
    }
    pub fn values(&self) -> impl Iterator<Item = &T> {
        hash::raw::values(&self.core)
    }
    pub fn entry(
        &mut self,
        key: key::Handle<runtime::Domain<Tag>>,
    ) -> Result<entry::Live<'_, T, Tag, P>, errors::Lookup<Tag>> {
        hash::raw::entry(self, key)
    }
    pub fn entry_at(&mut self, index: usize) -> Option<entry::Live<'_, T, Tag, P>> {
        hash::raw::entry_at(self, index)
    }
}

impl<T, Tag> Map<T, Tag> {
    pub const fn len(&self) -> usize {
        self.core.len as usize
    }
    pub fn key_at(&self, index: usize) -> Option<key::Handle<runtime::Domain<Tag>>> {
        let (index, stamp) = self.core.live_at(index)?;
        Some(key::Handle::new(index.get(), stamp))
    }
    pub fn try_entry<E>(
        &mut self,
        hash: u64,
        eq: impl Fn(&T) -> Result<bool, E>,
    ) -> Entry<'_, T, Tag, E> {
        Ok(
            match map::raw::Insertion::try_entry(&mut self.core, hash, eq)? {
                map::Entry::Occupied(entry) => map::Entry::Occupied(entry.occupied()),
                map::Entry::Vacant(entry) => map::Entry::Vacant(entry),
            },
        )
    }
    pub fn try_vacant(
        &mut self,
        hash: u64,
        eq: impl Fn(&T) -> bool,
    ) -> Result<entry::Vacancy<'_, T, Tag>, errors::Create<Tag>> {
        let capacity = self.capacity();
        match map::raw::Insertion::entry(&mut self.core, hash, eq) {
            map::Entry::Occupied(entry) => Err(errors::Create::Occupied { key: entry.key() }),
            map::Entry::Vacant(None) => Err(errors::Create::Full { capacity }),
            map::Entry::Vacant(Some(entry)) => Ok(entry),
        }
    }
    pub fn get_mut(&mut self, key: key::Handle<runtime::Domain<Tag>>) -> Option<&mut T> {
        Some(self.entry(key).ok()?.into_mut())
    }
}

impl<T, Tag> Map<T, Tag, ()> {
    pub fn vacant(&mut self) -> Option<hash::Vacant<'_, T, Tag>> {
        self.core.vacant()
    }
    pub fn find(
        &mut self,
        hash: u64,
        eq: impl Fn(&T) -> bool,
    ) -> Option<entry::Live<'_, T, Tag, entry::Indexed>> {
        map::raw::Query::find(&mut self.core, hash, eq)
    }
}

type Entry<'a, T, Tag, E> =
    Result<map::Entry<entry::Live<'a, T, Tag>, Option<entry::Vacancy<'a, T, Tag>>>, E>;