o3 0.62.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::{arena::linked, capacity, queue::pool};

use pool::Front;

/// Exclusive access to one exact FIFO lane and its state.
pub struct Lane<'a, T, S = (), const TRACK: bool = true> {
    pub(in crate::depot::queue::pool) store: &'a linked::raw::Store<T>,
    pub(in crate::depot::queue::pool) slot: &'a mut crate::depot::raw::QueueSlot<S>,
}

impl<'a, T, S, const TRACK: bool> Lane<'a, T, S, TRACK> {
    pub(in crate::depot) const fn new(
        store: &'a linked::raw::Store<T>,
        slot: &'a mut crate::depot::raw::QueueSlot<S>,
    ) -> Self {
        Self { store, slot }
    }

    /// Consumes the exact lane and its original unary insertion entitlement.
    pub fn insert<I>(source: I) -> &'a mut S
    where
        T: Copy,
        I: crate::depot::raw::PoolInsert<'a, TRACK, Item = T, State = S>,
    {
        // SAFETY: the admitted source retains this lane's unused slot entitlement.
        let (lane, item) = unsafe { source.into_parts() };
        let owner =
            unsafe { linked::raw::Bound::<_, _, TRACK>::new(&mut lane.slot.queue, lane.store) };
        unsafe { linked::raw::Vacant::copy_one(owner, item) }.publish::<false>();
        &mut lane.slot.state
    }

    /// # Safety
    /// This exact lane owns an available slot under its TRACK policy.
    pub(super) unsafe fn insert_at<const FRONT: bool, const BRANCH: u8>(
        self,
        value: impl crate::mem::raw::Convert<T, BRANCH>,
    ) -> &'a mut S {
        let owner =
            unsafe { linked::raw::Bound::<_, _, TRACK>::new(&mut self.slot.queue, self.store) };
        unsafe { linked::raw::Vacant::insert_one::<BRANCH>(owner, value) }.publish::<FRONT>();
        &mut self.slot.state
    }

    pub const fn count(&self) -> capacity::Count {
        self.slot.queue.count()
    }

    pub const fn is_empty(&self) -> bool {
        self.slot.queue.is_empty()
    }

    pub const fn state(&self) -> &S {
        &self.slot.state
    }

    pub const fn state_mut(&mut self) -> &mut S {
        &mut self.slot.state
    }

    pub fn into_state(self) -> &'a mut S {
        &mut self.slot.state
    }

    /// Reborrows this admitted lane without repeating its owner lookup.
    pub fn borrow_mut(&mut self) -> Lane<'_, T, S, TRACK> {
        Lane::new(self.store, self.slot)
    }

    pub fn front_entry(self) -> Option<Front<'a, T, S, TRACK>> {
        let index = self.slot.queue.links.head()?;
        Some(Front::new(self, index))
    }

    pub fn front_mut(&mut self) -> Option<Front<'_, T, S, TRACK>> {
        self.borrow_mut().front_entry()
    }

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

    /// Removes every value, returning each slot before destroying its payload.
    pub fn clear(&mut self) {
        while self.pop_front().is_some() {}
    }
}

impl<'a, T, S> Lane<'a, T, S> {
    /// Reserves one exact slot until insertion or cancellation.
    pub fn vacant(&mut self) -> Option<linked::raw::Vacant<T, linked::raw::Bound<'_, T>>> {
        // SAFETY: this lane owns its tracked queue in the exact store.
        let owner = unsafe { linked::raw::Bound::new(&mut self.slot.queue, self.store) };
        unsafe { linked::raw::Vacant::try_reserve(owner) }
    }

    /// Reserves a complete node count in this exact lane until append or cancellation.
    pub fn try_reserve(
        self,
        count: capacity::Count,
    ) -> Result<linked::raw::Reserve<T, linked::raw::Bound<'a, T>>, capacity::Shortage> {
        self.store.check(count)?;
        // SAFETY: this exclusive lane retains the complete count just admitted.
        Ok(unsafe { self.reserve(count) })
    }
}

const _: () = {
    assert!(mem::size_of::<Lane<'static, u64>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<crate::depot::raw::QueueSlot<()>>() == 3 * mem::size_of::<u32>());
};

// SAFETY: Head retains this exact lane's current initialized head until projection.
unsafe impl<'a, T, S, R, const TRACK: bool> crate::depot::raw::Project<(), pool::Head>
    for (Lane<'a, T, S, TRACK>, R)
{
    type Output = (pool::Front<'a, T, S, TRACK>, R);

    unsafe fn project(self, _: ()) -> Self::Output {
        let (lane, retained) = self;
        let index = lane.slot.queue.links.head_raw();
        (pool::Front::new(lane, index), retained)
    }
}

impl<'a, T, S> Lane<'a, T, S> {
    /// # Safety
    /// The same owner loan preserves at least `count` available slots in this store.
    pub unsafe fn reserve(
        self,
        count: capacity::Count,
    ) -> linked::raw::Reserve<T, linked::raw::Bound<'a, T>> {
        let owner = unsafe { linked::raw::Bound::new(&mut self.slot.queue, self.store) };
        unsafe { linked::raw::Reserve::new(owner, count) }
    }
}

// SAFETY: the key names a live member of this retained lane's unchanged partition.
unsafe impl<'a, T, S, const TRACK: bool> crate::depot::raw::Project<linked::raw::Index>
    for pool::View<'a, T, S, TRACK>
{
    type Output = linked::Iter<'a, T>;

    unsafe fn project(self, index: linked::raw::Index) -> Self::Output {
        unsafe { linked::Iter::new(self.store, index) }
    }
}