o3 0.62.0

shared-nothing primitives
Documentation
//! FIFO admission requiring one exact value per physical index.

use std::mem;

pub use bounds::Bounds;

use crate::depot::{index::Initialized, queue::indexed::fifo};

mod bounds;

/// One admitted identity retaining exclusive access to its original FIFO.
#[must_use]
pub struct Entry<'a, T: Copy + Eq, I: crate::depot::raw::Compact = u32> {
    pub(in crate::depot::queue) owner: &'a mut fifo::List<T, I>,
    pub(in crate::depot::queue) index: I,
}

impl<'a, T: Copy + Eq, I: crate::depot::raw::Compact> Entry<'a, T, I> {
    pub fn index(&self) -> I {
        self.index
    }

    /// Projects an admitted node with the complete joint source still borrowed.
    pub fn from_source<C>(source: Admission<'a, T, I, C>) -> (Self, C) {
        let ((owner, context), index) = source.into_inner();
        (Self { owner, index }, context)
    }

    pub fn schedule<const KEEP_EQUAL: bool>(&mut self, value: T) {
        // SAFETY: admission retains this represented identity in the original FIFO.
        let entry = unsafe { self.owner.store.at(self.index.into_raw() as usize) };
        if entry.is_linked::<0>() {
            // SAFETY: the exclusive loan preserves the occupied slot across Eq.
            if KEEP_EQUAL && unsafe { (*entry.value::<0>()).assume_init_read() } == value {
                return;
            }
            unsafe { entry.write_back::<0, true>(value) };
        } else {
            unsafe { entry.write_back::<0, false>(value) };
        }
    }

    pub fn unlink(&mut self, expected: T) {
        // SAFETY: admission retains this represented identity in the original FIFO.
        let entry = unsafe { self.owner.store.at(self.index.into_raw() as usize) };
        if entry.is_linked::<0>() && unsafe { (*entry.value::<0>()).assume_init_read() } == expected
        {
            // SAFETY: the exclusive loan prevents a replacement between Eq and removal.
            unsafe { entry.unlink::<0, false>() };
        }
    }
}

const _: [(); 2 * mem::size_of::<usize>()] = [(); mem::size_of::<Entry<'_, u64>>()];
const _: [(); 2 * mem::size_of::<usize>()] = [(); mem::size_of::<Option<Entry<'_, u64>>>()];

/// The source context and FIFO loan retain one admitted identity.
type Admission<'a, T, I, C> =
    crate::depot::raw::Proof<((&'a mut fifo::List<T, I>, C), I), Initialized<()>>;