o3 0.60.0

shared-nothing primitives
Documentation
//! Counted FIFO state over external linked slots.

use std::mem;

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

pub mod ends;

/// Compact counted FIFO state stored outside its linked node arena.
#[repr(C)]
pub struct Fifo<Tag = ()> {
    pub(in crate::depot) links: ends::Exclusive<slots::Index<Tag>>,
    len: u32,
}

impl<Tag> Default for Fifo<Tag> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Tag> Fifo<Tag> {
    pub const fn new() -> Self {
        Self {
            links: ends::Exclusive::empty(),
            len: 0,
        }
    }

    pub const fn len(&self) -> usize {
        self.len as usize
    }

    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn back(&self) -> Option<Back<Tag>> {
        let index = self.links.tail_raw();
        index.get()?;
        Some(Back { index })
    }

    /// # Safety
    /// The queue owns a disjoint live partition in this store. The detached slot has no successor.
    pub(in crate::depot) unsafe fn link<T, const FRONT: bool>(
        &mut self,
        store: &slots::Store<T>,
        index: slots::Index<Tag>,
    ) {
        if FRONT {
            unsafe { self.links.prepend_link(store, index) };
        } else {
            unsafe { self.links.append_end(store, index) };
        }
        self.len = self.len.wrapping_add(1);
    }

    /// # Safety
    /// The index is the unchanged front of this queue in its original store.
    pub(in crate::depot) unsafe fn unlink<T>(
        &mut self,
        store: &slots::Store<T>,
        index: slots::Index<Tag>,
    ) {
        unsafe { self.links.unlink(store, index) };
        self.len = self.len.wrapping_sub(1);
    }

    /// # Safety
    /// The index is the original front with this store's exact accounting policy.
    pub(in crate::depot) unsafe fn pop<T, const TRACK: bool>(
        &mut self,
        store: &slots::Store<T>,
        index: slots::Index<Tag>,
    ) -> T {
        unsafe { self.unlink(store, index) };
        unsafe { store.take::<_, TRACK>(index) }
    }

    pub(in crate::depot) const fn count(&self) -> depot::capacity::Count {
        depot::capacity::Count::new(self.len)
    }
}

const _: [(); mem::size_of::<Fifo>()] = [(); 3 * mem::size_of::<u32>()];

/// One observed live FIFO tail in a linked-node domain.
#[must_use]
#[repr(transparent)]
pub struct Back<Tag> {
    index: slots::Index<Tag>,
}

impl<Tag> Back<Tag> {
    pub(in crate::depot) const fn into_index(self) -> slots::Index<Tag> {
        self.index
    }
}

const _: [(); mem::size_of::<Back<()>>()] = [(); mem::size_of::<u32>()];

/// The retained front is current, or the retained FIFO may be empty.
/// The callback detaches that exact stable front, returns its exact stored successor,
/// and neither reenters nor invalidates the retained endpoints during the operation.
#[derive(Debug)]
pub enum Detach {}

/// The retained index is a valid detached slot disjoint from the stable destination chain.
/// Its successor already equals the supplied exact previous head, which remains unchanged.
/// Neither the slot nor the destination is reentered before endpoint publication.
#[derive(Debug)]
pub enum Prepend {}

/// The retained index is a valid detached tail slot disjoint from the stable destination chain.
/// Its successor is absent. Link the exact prior tail before publishing endpoints;
/// supplied prior-tail facts remain unchanged without reentry or invalidation.
#[derive(Debug)]
pub enum Append {}

/// The exact endpoints delimit a stable nonempty source disjoint from the destination.
/// Its ownership transfers to that destination in the same store.
/// The supplied previous tail is unchanged and, when present, already links to the source head.
/// Neither chain is reentered or invalidated before endpoint publication.
#[derive(Debug)]
pub enum Join {}

impl depot::raw::Sealed for Detach {}
impl depot::raw::Sealed for Prepend {}
impl depot::raw::Sealed for Append {}
impl depot::raw::Sealed for Join {}