o3 0.60.0

shared-nothing primitives
Documentation
use std::{marker, ptr};

use crate::depot::{map::direct, queue::links, raw::Live};

/// A direct-address slot validated occupied for one exact key.
pub struct Occupied<'a, K: crate::depot::raw::Key, V, P = ()> {
    pub(in crate::depot::map::direct) table: &'a mut direct::Table<K, V>,
    pub(in crate::depot::map::direct) key: K,
    pub(in crate::depot::map::direct) plane: marker::PhantomData<fn(P) -> P>,
}

impl<'a, K: crate::depot::raw::Key, V> Occupied<'a, K, V> {
    pub fn from_live<C, P>(
        live: Live<(&'a mut direct::Table<K, V>, C), links::Index, P>,
    ) -> (Occupied<'a, K, V, P>, C) {
        let ((table, context), index) = live.into_inner();
        // SAFETY: the consumed live proof retains this initialized slot's exact key.
        let (key, _) = unsafe { direct::raw::Entry::occupied(table, index.get()) };
        (
            Occupied {
                table,
                key,
                plane: marker::PhantomData,
            },
            context,
        )
    }
}

impl<'a, K: crate::depot::raw::Key, V, P> Occupied<'a, K, V, P> {
    pub fn get_mut(&mut self) -> &mut V {
        unsafe { self.slot_mut().value.assume_init_mut() }
    }

    pub fn key(&self) -> K {
        self.key
    }

    pub fn table(&self) -> &direct::Table<K, V> {
        self.table
    }

    fn slot(&self) -> &direct::raw::Entry<K, V> {
        unsafe { self.table.block.get_unchecked(self.key.index()) }
    }

    fn slot_mut(&mut self) -> &mut direct::raw::Entry<K, V> {
        unsafe { self.table.block.get_unchecked_mut(self.key.index()) }
    }

    fn into_slot(self) -> &'a mut direct::raw::Entry<K, V> {
        unsafe { self.table.block.get_unchecked_mut(self.key.index()) }
    }

    pub fn get(&self) -> &V {
        unsafe { self.slot().value.assume_init_ref() }
    }

    pub fn into_mut(self) -> &'a mut V {
        unsafe { self.into_slot().value.assume_init_mut() }
    }

    pub fn remove(self) -> V {
        self.take().1
    }

    fn vacate(&mut self) -> ptr::NonNull<V> {
        // SAFETY: this owner retains the exact initialized row in its original table.
        let row =
            ptr::NonNull::from(unsafe { self.table.block.get_unchecked_mut(self.key.index()) });
        unsafe { self.table.vacate(row) }
    }

    /// Disarms value destruction while retaining the exact vacant table loan.
    pub fn into_uninit(mut self) -> direct::Vacant<'a, K, V, P> {
        self.vacate();
        direct::Vacant {
            table: self.table,
            key: self.key,
            plane: marker::PhantomData,
        }
    }

    pub fn take(mut self) -> (direct::Vacant<'a, K, V, P>, V) {
        // SAFETY: vacating transfers this original value's sole destruction right.
        let value = unsafe { self.vacate().as_ptr().read() };
        (
            direct::Vacant {
                table: self.table,
                key: self.key,
                plane: marker::PhantomData,
            },
            value,
        )
    }

    pub fn discard(mut self) {
        // SAFETY: the table no longer owns this exact resident value.
        unsafe { self.vacate().as_ptr().drop_in_place() };
    }
}