o3 0.61.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::arena::linked::{head, slots};

/// The unchanged list front tied to its store and owner borrows.
#[must_use]
pub struct Entry<'a, 'b, T, Tag> {
    store: &'a slots::Store<T>,
    state: &'b mut head::List<Tag>,
}

impl<'a, 'b, T, Tag> Entry<'a, 'b, T, Tag> {
    /// # Safety
    /// The state owns a nonempty acyclic live partition in this exact store.
    pub(in crate::depot::arena::linked) const unsafe fn new(
        store: &'a slots::Store<T>,
        state: &'b mut head::List<Tag>,
    ) -> Self {
        Self { store, state }
    }

    pub fn get(&self) -> &T {
        unsafe { self.store.value(self.state.head) }
    }

    pub fn pop(self) -> T {
        self.take().0
    }

    pub fn take(self) -> (T, slots::Vacant<T, &'a slots::Store<T>, Tag>) {
        let index = self.state.head;
        self.state.head = unsafe { self.store.next(index) };
        unsafe { self.store.detached(index) }.take()
    }
}

const _: () =
    assert!(mem::size_of::<Entry<'static, 'static, u64, ()>>() == 2 * mem::size_of::<usize>());

pub(crate) struct Drain<'a, T, Tag = ()> {
    store: &'a slots::Store<T>,
    next: slots::Index<Tag>,
}

impl<'a, T, Tag> Drain<'a, T, Tag> {
    /// # Safety
    /// Transfer the complete, disjoint, tracked partition starting at `next` in this store.
    pub(crate) unsafe fn new(store: &'a slots::Store<T>, next: slots::Index<Tag>) -> Self {
        Self { store, next }
    }
}

impl<'a, T, Tag> Iterator for Drain<'a, T, Tag> {
    type Item = slots::Live<T, &'a slots::Store<T>, Tag>;

    fn next(&mut self) -> Option<Self::Item> {
        let head = self.next;
        head.get()?;
        // SAFETY: keep the successor before transferring this initialized slot and its return right.
        self.next = unsafe { self.store.next(head) };
        Some(unsafe { self.store.detached(head) })
    }
}

impl<T, Tag> Drop for Drain<'_, T, Tag> {
    fn drop(&mut self) {
        for _ in self {}
    }
}