o3 0.59.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::arena::linked) 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 _: () = assert!(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 _: () = assert!(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.
/// The callback stores the exact former front as its successor before publication,
/// without reentry or invalidating either the slot or destination endpoints.
#[derive(Debug)]
pub enum Prepend {}

/// The retained index is a valid detached tail slot disjoint from the stable destination chain.
/// The callback links the exact former tail to it before endpoint publication,
/// without reentry or invalidating either the slot or destination endpoints.
#[derive(Debug)]
pub enum Append {}

/// Both retained chains are stable, valid, disjoint partitions in one exact slot store.
/// The callback links the destination's exact former tail to the source's exact front,
/// without reentry or invalidating either chain before their endpoints are published.
#[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 {}