o3 0.61.0

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

pub use error::Error;

use crate::depot::{
    array::Index,
    map::direct,
    queue::{indexed::lanes, links},
};

mod error;

/// One direct row and its topology retained by the original exclusive table loan.
pub struct Slot<'a, K: crate::depot::raw::Key, V, const N: usize, const LIVE: bool> {
    pub(super) root: &'a mut lanes::Table<K, V, N>,
    pub(super) key: K,
    pub(super) row: ptr::NonNull<direct::raw::Entry<K, lanes::Node<V, N>>>,
}

impl<'a, K: crate::depot::raw::Key, V, const N: usize> Occupied<'a, K, V, N> {
    fn node(&self) -> &lanes::Node<V, N> {
        // SAFETY: this owner retains the initialized row selected from this root.
        unsafe { self.row.as_ref().value.assume_init_ref() }
    }

    pub fn get(&self) -> &V {
        &self.node().value
    }

    pub fn get_mut(&mut self) -> &mut V {
        // SAFETY: this exclusive owner issues only this short payload loan.
        &mut unsafe { self.row.as_mut().value.assume_init_mut() }.value
    }

    pub fn into_mut(self) -> &'a mut V {
        // SAFETY: consuming this owner transfers the original table's exclusive loan.
        &mut unsafe { (*self.row.as_ptr()).value.assume_init_mut() }.value
    }

    pub fn move_to_back(&mut self, target: Index<N>) {
        // SAFETY: the retained key identifies this admitted row's physical index.
        let index = unsafe { links::Index::new_usize(self.key.index()) };
        let hooks = &self.root.values;
        let node = self.node();
        let queue = target.get(&self.root.queues);
        // SAFETY: this row owns its original hook and current lane.
        let vacant = match node.lane.get() {
            Some(source) if source == target => {
                return unsafe { queue.rotate(hooks, index, &node.hook, false) };
            }
            Some(source) => {
                let chain = source.get(&self.root.queues);
                unsafe { chain.detach_hook::<false, 0, _>(hooks, index, &node.hook) }.0
            }
            None => unsafe { links::Detached::new(&node.hook, index) },
        };
        unsafe { queue.push(hooks, vacant) };
        node.lane.set(Some(target));
    }

    pub fn move_all(&mut self, source: Index<N>, target: Index<N>) -> usize {
        lanes::table::move_all(&self.root.values, &self.root.queues, source, target)
    }

    pub fn unlink(self) {
        self.detach::<true>();
    }

    pub fn discard(self) {
        self.detach::<false>();
        // SAFETY: the same original row is unlinked before its metadata and value retire.
        unsafe { self.root.values.vacate(self.row).as_ptr().drop_in_place() };
    }

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

    pub fn lane(&self) -> Option<Index<N>> {
        self.node().lane.get()
    }

    fn detach<const IDLE: bool>(&self) {
        // SAFETY: this exact row keeps its lane and hook initialized through retirement,
        // independently of the resident payload.
        let (current, hook) = unsafe {
            let node = ptr::addr_of!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>();
            (&*ptr::addr_of!((*node).lane), &*ptr::addr_of!((*node).hook))
        };
        if let Some(lane) = current.get() {
            // SAFETY: the key and hook belong to this root's exact occupied row.
            let index = unsafe { links::Index::new_usize(self.key.index()) };
            let detached = unsafe {
                lane.get(&self.root.queues).detach_hook::<false, 0, _>(
                    &self.root.values,
                    index,
                    hook,
                )
            }
            .0;
            if IDLE {
                detached.idle();
                current.set(None);
            }
        }
    }

    /// Retires metadata without reading or dropping the resident payload.
    pub fn into_uninit(self) -> Vacancy<'a, K, V, N> {
        self.detach::<false>();
        // SAFETY: the original key and initialized metadata retain this exact exclusive row.
        unsafe { self.root.values.vacate(self.row) };
        Slot {
            root: self.root,
            key: self.key,
            row: self.row,
        }
    }

    /// Transfers destruction of the same resident value after metadata retirement.
    pub fn into_owner(self) -> crate::mem::slot::Occupied<'a, V> {
        let mut vacant = self.into_uninit();
        let value = vacant.as_mut_ptr();
        std::mem::forget(vacant);
        unsafe {
            crate::mem::slot::Vacant::new(&mut *value.as_ptr().cast::<std::mem::MaybeUninit<V>>())
                .assume_init()
        }
    }

    pub fn remove(self) -> V {
        self.into_owner().into_inner()
    }
}

impl<'a, K: crate::depot::raw::Key, V, const N: usize> Vacancy<'a, K, V, N> {
    pub fn as_mut_ptr(&mut self) -> ptr::NonNull<V> {
        let node =
            unsafe { ptr::addr_of_mut!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>() };
        // SAFETY: this original vacancy owns the whole node; no reference is created.
        unsafe { ptr::NonNull::new_unchecked(ptr::addr_of_mut!((*node).value)) }
    }

    /// # Safety
    /// This vacancy's exact payload is fully initialized without another owner or loan.
    pub unsafe fn assume_init(self) -> Occupied<'a, K, V, N> {
        // SAFETY: this key and row retain the same admitted root and physical index.
        let index = unsafe { links::Index::new_usize(self.key.index()) };
        let node =
            unsafe { ptr::addr_of_mut!((*self.row.as_ptr()).value).cast::<lanes::Node<V, N>>() };
        unsafe {
            ptr::addr_of_mut!((*node).lane).write(cell::Cell::new(None));
            ptr::addr_of_mut!((*node).hook).write(links::Hook::new(index));
            self.root.values.publish(self.row, self.key);
        }
        Slot {
            root: self.root,
            key: self.key,
            row: self.row,
        }
    }

    pub fn insert(mut self, value: V) -> Occupied<'a, K, V, N> {
        // SAFETY: this vacancy retains the only write and publication right for this payload.
        unsafe { self.as_mut_ptr().as_ptr().write(value) };
        unsafe { self.assume_init() }
    }
}

pub type Occupied<'a, K, V, const N: usize> = Slot<'a, K, V, N, true>;

pub type Vacancy<'a, K, V, const N: usize> = Slot<'a, K, V, N, false>;