o3 0.62.0

shared-nothing primitives
Documentation
use std::{marker, mem, num::NonZero};

use crate::depot::map::Entry;

use crate::depot::{
    capacity,
    queue::{
        self,
        indexed::{Live, exact, fifo},
        links,
    },
};

pub mod store;

/// A fixed-capacity FIFO whose physical index is supplied by the caller.
pub struct List<T = (), I: crate::depot::raw::Compact = u32> {
    pub(in crate::depot::queue) store: store::Store<T>,
    index: marker::PhantomData<fn() -> I>,
}

#[derive(Clone, Copy)]
struct Counted<I>(capacity::Count, marker::PhantomData<fn(I) -> I>);

impl<I: crate::depot::raw::Compact> Counted<I> {
    fn try_new(capacity: usize) -> Result<Self, crate::alloc::Error> {
        let count =
            capacity::Count::try_index(capacity, I::MAX as usize, crate::alloc::Error::Overflow)?;
        Ok(Self(count, marker::PhantomData))
    }
}

// SAFETY: the private constructor admits only positions representable by I.
unsafe impl<I: crate::depot::raw::Compact> crate::depot::raw::Count<I> for Counted<I> {
    fn count(self) -> capacity::Count {
        self.0
    }
}

impl<T> List<T> {
    /// Allocates a nonempty domain and queues its first physical slot.
    pub fn try_with(count: NonZero<u32>, first: T) -> Result<Self, crate::alloc::Error> {
        let queue = Self::try_count(count)?;
        let index = links::Index::from(NonZero::<u32>::MIN);
        // SAFETY: the retained nonzero count admits this initialized vacant slot.
        let entry = unsafe { queue.store.at(index.get()) };
        // SAFETY: construction leaves every plane vacant before this first publication.
        unsafe { entry.write_back::<0, false>(first) };
        Ok(queue)
    }
}

impl<T, I: crate::depot::raw::Compact> List<T, I> {
    pub fn try_new(capacity: usize) -> Result<Self, crate::alloc::Error> {
        Self::try_count(Counted::try_new(capacity)?)
    }

    pub fn try_count<C: crate::depot::raw::Count<I>>(
        count: C,
    ) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            store: store::Store::try_count(count.count(), |_index| ())?,
            index: marker::PhantomData,
        })
    }

    pub fn pop_front(&mut self) -> Option<T> {
        Some(self.pop_head()?.1)
    }

    pub(in crate::depot::queue::indexed) fn pop_head(&mut self) -> Option<(links::Index, T)> {
        let head = self.store.queue::<0>().head()?;
        // SAFETY: this exclusive loan retains the original initialized head.
        Some((head.position(), unsafe { head.take::<0, true>() }))
    }

    pub fn len(&self) -> usize {
        self.store.queue::<0>().chain().len()
    }

    pub fn capacity(&self) -> usize {
        self.store.capacity()
    }

    pub fn is_empty(&self) -> bool {
        self.store.queue::<0>().chain().is_empty()
    }

    pub fn entry(&mut self, index: I) -> Option<store::Membership<'_, T, (), 1, 0>> {
        let slot = self.store.get(index.into_raw() as usize)?;
        // SAFETY: the original exclusive FIFO loan retains this plane until the result is consumed.
        Some(unsafe { slot.classify::<0>() })
    }

    pub fn vacant_entry(&mut self, index: I) -> Option<store::Plane<'_, T, (), 1, 0, false>> {
        match self.entry(index)? {
            Entry::Vacant(slot) => Some(slot),
            Entry::Occupied(_) => None,
        }
    }
}

const _: () = {
    assert!(mem::size_of::<List<u64>>() == 3 * mem::size_of::<usize>());
    assert!(
        mem::size_of::<store::Plane<'_, u64, (), 1, 0, false>>() == 2 * mem::size_of::<usize>()
    );
    assert!(mem::size_of::<store::Plane<'_, u64, (), 1, 0, true>>() == 2 * mem::size_of::<usize>());
};

impl<T: Copy + Eq, I: crate::depot::raw::Compact> fifo::List<T, I> {
    pub fn sweep(&mut self, limit: usize) -> queue::Sweep<'_, Self> {
        let left = limit.min(self.len());
        queue::Sweep {
            owner: self,
            remaining: left,
        }
    }

    pub fn try_entry(&mut self, index: I) -> Result<exact::Entry<'_, T, I>, exact::Bounds<I>> {
        let capacity = self.store.capacity();
        if index.into_raw() as usize >= capacity {
            return Err(exact::Bounds { index, capacity });
        }
        Ok(exact::Entry { owner: self, index })
    }

    pub fn front(&self) -> Option<T> {
        self.store.queue::<0>().front()
    }

    /// Removes the head while retaining its original physical-slot admission.
    pub fn pop(&mut self) -> Option<(Live<&mut Self, I>, T)> {
        let (index, value) = self.pop_head()?;
        // SAFETY: the retained owner still contains the exact initialized physical node.
        Some((
            unsafe { crate::depot::raw::Proof::new((self, I::from_raw(index.raw()))) },
            value,
        ))
    }
}