o3 0.60.0

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

use crate::depot::{
    index::{Identity, Stored},
    map::direct,
};

/// One direct-address slot whose key records whether its value is initialized.
#[repr(C)]
pub(in crate::depot) struct Entry<K: crate::depot::raw::Key, V> {
    pub(in crate::depot) key: K::Raw,
    pub(in crate::depot) value: mem::MaybeUninit<V>,
    domain: Identity<K>,
}

impl<K: crate::depot::raw::Key, V> Entry<K, V> {
    pub(in crate::depot) fn vacant() -> Self {
        Self {
            key: K::VACANT,
            value: mem::MaybeUninit::uninit(),
            domain: marker::PhantomData,
        }
    }

    pub(in crate::depot) fn read(&self) -> Option<(K, &V)> {
        (self.key != K::VACANT).then(|| unsafe { self.live() })
    }

    /// # Safety
    /// This slot retains its emitted key and initialized value for the loan.
    pub(in crate::depot) unsafe fn live(&self) -> (K, &V) {
        let key = K::from_raw(unsafe { Stored::new(self.key) });
        (key, unsafe { self.value.assume_init_ref() })
    }

    /// # Safety
    /// `index` names an occupied slot in this exact table.
    pub(in crate::depot) unsafe fn occupied(
        table: &mut direct::Table<K, V>,
        index: usize,
    ) -> (K, ptr::NonNull<Self>) {
        let row = unsafe { table.block.get_unchecked_mut(index) };
        let key = unsafe { row.live() }.0;
        (key, ptr::NonNull::from(row))
    }
}

impl<K: crate::depot::raw::Key, V> Drop for Entry<K, V> {
    fn drop(&mut self) {
        if self.key == K::VACANT {
            return;
        }
        // SAFETY: this slot's occupied key retains its one initialized value.
        unsafe { self.value.assume_init_drop() };
    }
}