o3 0.61.0

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

use crate::depot::map::direct;

/// A direct-address slot validated vacant for one exact key.
#[must_use = "a vacant entry does nothing unless inserted"]
pub struct Vacant<'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, P> Vacant<'a, K, V, P> {
    pub fn key(&self) -> K {
        self.key
    }

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

    pub fn as_mut_ptr(&mut self) -> ptr::NonNull<V> {
        // SAFETY: this exact vacancy retains its admitted allocation and index.
        unsafe {
            let entry = self.table.block.as_mut_ptr().add(self.key.index());
            ptr::NonNull::new_unchecked(ptr::addr_of_mut!((*entry).value).cast())
        }
    }

    /// # Safety
    /// This vacancy's exact value is initialized and has no other owner or loan.
    pub unsafe fn assume_init(self) -> direct::Occupied<'a, K, V> {
        // SAFETY: this original vacancy retains the fully initialized row.
        let row =
            ptr::NonNull::from(unsafe { self.table.block.get_unchecked_mut(self.key.index()) });
        unsafe { self.table.publish(row, self.key) };
        direct::Occupied {
            table: self.table,
            key: self.key,
            plane: marker::PhantomData,
        }
    }

    pub fn insert(mut self, value: V) -> direct::Occupied<'a, K, V> {
        // SAFETY: the vacancy owns this exact uninitialized destination.
        unsafe { self.as_mut_ptr().as_ptr().write(value) };
        unsafe { self.assume_init() }
    }
}