o3 0.62.0

shared-nothing primitives
Documentation
//! FIFO endpoints over caller-owned links.

use std::{cell, mem};

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

/// Move-only ownership of one external forward chain.
#[repr(transparent)]
#[must_use = "dropping an exclusive chain abandons ownership of its external links"]
#[derive(Debug)]
pub struct Exclusive<L: depot::raw::Link = u32>(pub(in crate::depot::arena::linked) [L; 2]);

/// Interior-mutable FIFO topology over external links.
#[repr(transparent)]
pub struct Cell<L: depot::raw::Link = u32>(cell::Cell<[L; 2]>);

/// The observed front and its original endpoint storage.
#[must_use]
pub struct Front<'a, L: depot::raw::Link = u32> {
    ends: &'a cell::Cell<[L; 2]>,
    present: L::Item,
}

impl<'a, L: depot::raw::Link> Front<'a, L> {
    fn new(ends: &'a cell::Cell<[L; 2]>) -> Option<Self> {
        let present = ends.as_array_of_cells()[0].get().item()?;
        Some(Self { ends, present })
    }

    pub fn get(&self) -> L::Item {
        self.present
    }

    /// Publishes the detached node's stored successor and retains it for traversal.
    pub fn advance(proof: depot::raw::Proof<(Self, L), fifo::Detach>) -> Option<Self> {
        let (Self { ends, .. }, next) = proof.into_inner();
        let present = next.item();
        publish::<true, _>(ends, next, L::NONE, present.is_none());
        Some(Self {
            ends,
            present: present?,
        })
    }
}

fn publish<const FRONT: bool, L: Copy>(
    ends: &cell::Cell<[L; 2]>,
    head: L,
    tail: L,
    opposite: bool,
) {
    let [first, last] = ends.as_array_of_cells();
    if FRONT || opposite {
        first.set(head);
    }
    if !FRONT || opposite {
        last.set(tail);
    }
}

impl<L: depot::raw::Link> Exclusive<L> {
    pub const fn empty() -> Self {
        Self([L::NONE; 2])
    }

    pub fn head(&self) -> Option<L::Item> {
        self.head_raw().item()
    }

    pub fn back(&self) -> Option<L::Item> {
        self.tail_raw().item()
    }

    pub(crate) const fn head_raw(&self) -> L {
        self.0[0]
    }

    pub(crate) const fn tail_raw(&self) -> L {
        self.0[1]
    }

    pub fn is_empty(&self) -> bool {
        self.head_raw() == L::NONE
    }

    pub fn front(&mut self) -> Option<Front<'_, L>> {
        Front::new(cell::Cell::from_mut(&mut self.0))
    }

    pub fn push_back(proof: fifo::Insertion<&mut Self, L::Item, false>) {
        let (owner, link, empty) = depot::raw::inserted::<L, _, false>(proof);
        publish::<false, _>(cell::Cell::from_mut(&mut owner.0), link, link, empty);
    }
}

impl<L: depot::raw::Link> Cell<L> {
    pub const fn empty() -> Self {
        Self(cell::Cell::new([L::NONE; 2]))
    }

    /// # Safety
    /// Both ends delimit one valid chain in its original store, or both are absent.
    pub(in crate::depot) const unsafe fn from_parts(head: L, tail: L) -> Self {
        Self(cell::Cell::new([head, tail]))
    }

    pub fn head(&self) -> Option<L::Item> {
        self.0.as_array_of_cells()[0].get().item()
    }

    pub fn back(&self) -> Option<L::Item> {
        self.0.as_array_of_cells()[1].get().item()
    }

    pub fn is_empty(&self) -> bool {
        self.0.as_array_of_cells()[0].get() == L::NONE
    }

    pub fn front(&self) -> Option<Front<'_, L>> {
        Front::new(&self.0)
    }

    pub fn push_front(proof: fifo::Insertion<&Self, L::Item, true>) {
        let (owner, link, empty) = depot::raw::inserted::<L, _, true>(proof);
        publish::<true, _>(&owner.0, link, link, empty);
    }

    pub fn push_back(proof: fifo::Insertion<&Self, L::Item, false>) {
        let (owner, link, empty) = depot::raw::inserted::<L, _, false>(proof);
        publish::<false, _>(&owner.0, link, link, empty);
    }

    pub fn append(proof: fifo::Joined<&Self, L>) {
        let (owner, head, tail, previous) = proof.into_inner();
        publish::<false, _>(&owner.0, head, tail, previous.is_none());
    }
}

unsafe impl<L: depot::raw::Link> depot::raw::EndsPop<L> for &mut Exclusive<L> {
    unsafe fn advance(self, next: L) {
        publish::<true, _>(
            cell::Cell::from_mut(&mut self.0),
            next,
            L::NONE,
            next == L::NONE,
        );
    }
}

unsafe impl<L: depot::raw::Link> depot::raw::EndsPop<L> for &Cell<L> {
    unsafe fn advance(self, next: L) {
        publish::<true, _>(&self.0, next, L::NONE, next == L::NONE);
    }
}

const _: () = {
    assert!(mem::size_of::<Exclusive<u16>>() == 2 * mem::size_of::<u16>());
    assert!(mem::size_of::<Exclusive<u32>>() == 2 * mem::size_of::<u32>());
    assert!(mem::size_of::<Cell<u16>>() == 2 * mem::size_of::<u16>());
    assert!(mem::size_of::<Cell<u32>>() == 2 * mem::size_of::<u32>());
    assert!(mem::align_of::<Exclusive<u32>>() == mem::align_of::<[u32; 2]>());
    assert!(mem::size_of::<Option<Exclusive<u16>>>() == mem::size_of::<Option<[u16; 2]>>());
    assert!(mem::size_of::<Option<Exclusive<u32>>>() == mem::size_of::<Option<[u32; 2]>>());
    assert!(mem::size_of::<Front<'_, u32>>() == 2 * mem::size_of::<usize>());
};