o3 0.61.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::{
    arena::{
        linked,
        linked::{fifo::ends, slots},
    },
    raw::{Arena, Proof},
};

/// An independently movable FIFO borrowing its exact fixed node pool.
pub struct Fifo<'pool, T> {
    pub(in crate::depot) arena: &'pool Arena<T>,
    pub(in crate::depot) links: ends::Exclusive<slots::Index>,
}

impl<'pool, T> Fifo<'pool, T> {
    pub(in crate::depot) const fn new(arena: &'pool Arena<T>) -> Self {
        Self {
            arena,
            links: ends::Exclusive::empty(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.links.is_empty()
    }

    pub fn pop_front(&mut self) -> Option<T> {
        let index = self.links.head()?;
        let node =
            Arena::fifo_detach(unsafe { Proof::new(((self.arena, &mut self.links), index)) });
        Some(node.into_inner())
    }

    pub fn iter(&self) -> linked::Iter<'_, T> {
        // SAFETY: this FIFO borrow retains its exact arena partition unchanged.
        unsafe { self.arena.fifo_iter(&self.links) }
    }

    pub fn vacant(&mut self) -> Option<slots::Vacant<T, &mut Fifo<'pool, T>>> {
        // SAFETY: this FIFO uses its store's tracked admission and return.
        unsafe { slots::Vacant::try_reserve(self) }
    }
}

// SAFETY: the borrowed FIFO retains its original allocation until release.
impl<T> linked::Sealed<T> for &mut Fifo<'_, T> {}

impl<T> linked::Source<T> for &mut Fifo<'_, T> {
    fn store(&self) -> &slots::Store<T> {
        &self.arena.inner
    }
}

impl<T> Drop for Fifo<'_, T> {
    fn drop(&mut self) {
        while self.pop_front().is_some() {}
    }
}

impl<'fifo, 'pool, T> slots::Vacant<T, &'fifo mut Fifo<'pool, T>> {
    /// Initializes and publishes this reserved node into its original FIFO.
    pub fn push_back(self, value: T) {
        let (fifo, index) = unsafe { self.insert(value).into_raw_parts() };
        unsafe { fifo.links.append_end(&fifo.arena.inner, index) };
    }
}

const _: () = {
    let _: [(); mem::size_of::<Fifo<'static, u64>>()] = [(); 2 * mem::size_of::<usize>()];
    let _: [(); mem::size_of::<slots::Vacant<u64, &'static mut Fifo<'static, u64>>>()] =
        [(); 2 * mem::size_of::<usize>()];
};