o3 0.62.0

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

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

/// 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(in crate::depot::queue::indexed::lanes) root: &'a mut lanes::Table<K, V, N>,
    pub(in crate::depot::queue::indexed::lanes) key: K,
    pub(in crate::depot::queue::indexed::lanes) row:
        ptr::NonNull<direct::raw::Entry<K, lanes::Node<V, N>>>,
}

impl<'a, K: crate::depot::raw::Key, V, const N: usize> Slot<'a, K, V, N, true> {
    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_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 => {
                unsafe { queue.rotate(hooks, index, &node.hook, false) };
                return;
            }
            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 {
        table::move_all(&self.root.values, &self.root.queues, source, target)
    }

    pub fn unlink<const RETIRE: bool>(self) {
        if RETIRE {
            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() };
        } else {
            self.detach::<true>();
        }
    }

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

    pub fn remove(self) -> V {
        self.detach::<false>();
        // SAFETY: metadata retires before the initialized payload is transferred once.
        let node = unsafe { self.root.values.vacate(self.row) };
        unsafe { ptr::addr_of!((*node.as_ptr()).value).read() }
    }
}

impl<'a, K: crate::depot::raw::Key, V, const N: usize> Slot<'a, K, V, N, false> {
    pub fn insert(mut self, value: V) -> Slot<'a, K, V, N, true> {
        // 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() }
    }
}

impl<'a, K: crate::depot::raw::Key, V, const N: usize, const LIVE: bool> Slot<'a, K, V, N, LIVE> {
    pub fn key(&self) -> K {
        self.key
    }

    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)) }
    }

    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) -> Slot<'a, K, V, N, false> {
        if LIVE {
            self.detach::<false>();
            // SAFETY: LIVE retains this row's initialized metadata, independently of V.
            unsafe { self.root.values.vacate(self.row) };
        }
        Slot {
            root: self.root,
            key: self.key,
            row: self.row,
        }
    }

    /// # Safety
    /// This vacancy's exact payload is fully initialized without another owner or loan.
    pub unsafe fn assume_init(self) -> Slot<'a, K, V, N, true> {
        if !LIVE {
            // 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,
        }
    }
}