o3 0.62.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::arena::linked::{self, fifo::ends};

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

impl<'pool, T> Fifo<'pool, T> {
    pub const fn new(arena: &'pool crate::depot::raw::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 = crate::depot::raw::Arena::fifo_detach(unsafe {
            crate::depot::raw::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<linked::raw::Vacant<T, &mut Fifo<'pool, T>>> {
        // SAFETY: this FIFO uses its store's tracked admission and return.
        unsafe { linked::raw::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) -> &linked::raw::Store<T> {
        &self.arena.inner
    }
}

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

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