o3 0.62.0

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

use crate::depot::map::direct::{self, sealed::Live};

/// A direct-address row admitted occupied in its original table.
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) row: ptr::NonNull<direct::raw::Entry<K, V>>,
    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), P>,
    ) -> (Occupied<'a, K, V, P>, C) {
        let ((table, context), index) = live.into_inner();
        // SAFETY: the consumed proof retains this occupied row in this exact allocation.
        let row = unsafe { ptr::NonNull::new_unchecked(table.block.as_mut_ptr().add(index.get())) };
        (
            Occupied {
                table,
                row,
                plane: marker::PhantomData,
            },
            context,
        )
    }
}

impl<'a, K: crate::depot::raw::Key, V, P> Occupied<'a, K, V, P> {
    pub fn key(&self) -> K {
        // SAFETY: only the resident key is read, independently of any payload loan.
        unsafe { direct::raw::Entry::identity(self.row) }
    }

    pub fn get(&self) -> &V {
        unsafe { direct::raw::Entry::payload(self.row).as_ref() }
    }

    pub fn get_mut(&mut self) -> &mut V {
        unsafe { direct::raw::Entry::payload(self.row).as_mut() }
    }

    pub fn into_mut(self) -> &'a mut V {
        unsafe { direct::raw::Entry::payload(self.row).as_mut() }
    }

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

    pub fn take(self) -> (direct::raw::Vacant<'a, K, V, P>, V) {
        let row = self.row;
        let vacant = self.into_uninit();
        // SAFETY: the table relinquished its destruction right to this value.
        (vacant, unsafe { direct::raw::Entry::payload(row).read() })
    }

    pub fn remove(self) -> V {
        unsafe { self.table.vacate(self.row).read() }
    }

    pub fn discard(self) {
        unsafe { self.table.vacate(self.row).drop_in_place() };
    }
}