o3 0.59.0

shared-nothing primitives
Documentation
//! Explicit unsafe boundary for compact external linked states.

use std::{marker, mem};

pub use cursor::Cursor;
pub use entry::Entry;
pub use index::Index;
pub use reserve::Reserve;
pub use slot::{Live, Vacant};
pub use store::Store;
pub(crate) use store::{Capacity, Input, Node};

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

pub mod fixed;
mod index;
mod slot;
mod store;

mod cursor;
mod entry;
mod reserve;

/// Exclusive proof that one exact node set has insertion capacity.
#[must_use]
pub struct Permit<'a, T, Tag> {
    pub(in crate::depot::arena::linked) store: &'a mut slots::Store<T>,
    domain: marker::PhantomData<fn(Tag) -> Tag>,
}

impl<'a, T, Tag> Permit<'a, T, Tag> {
    pub(super) const fn new(store: &'a mut slots::Store<T>) -> Self {
        Self {
            store,
            domain: marker::PhantomData,
        }
    }

    /// Initializes the slot proved available by this permit.
    pub fn claim(self, value: T) -> Live<T, &'a Store<T>, Tag> {
        unsafe { slots::Vacant::insert_one(&*self.store, value) }
    }
}

const _: () = {
    assert!(mem::size_of::<Permit<'static, u64, ()>>() == mem::size_of::<usize>());
    assert!(mem::size_of::<Option<Permit<'static, u64, ()>>>() == mem::size_of::<usize>());
    assert!(mem::size_of::<Vacant<u64, &'static Store<u64>, ()>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Live<u64, &'static Store<u64>, ()>>() == 2 * mem::size_of::<usize>());
};

/// A store borrow whose accounting is fixed by its exclusive topology owner.
pub struct Borrow<'a, T, const TRACK: bool>(pub(crate) &'a Store<T>);

// SAFETY: topology owners keep this fixed policy from insertion through removal.
impl<T, const TRACK: bool> linked::Sealed<T> for Borrow<'_, T, TRACK> {}

impl<T, const TRACK: bool> linked::Source<T> for Borrow<'_, T, TRACK> {
    const TRACK: bool = TRACK;

    fn store(&self) -> &Store<T> {
        self.0
    }
}

pub struct Bound<'a, T, Tag = (), const TRACK: bool = true> {
    store: &'a slots::Store<T>,
    state: &'a mut fifo::Fifo<Tag>,
}

impl<Tag> fifo::Fifo<Tag> {
    /// This state's live partition belongs to this exact store with the
    /// unchanged TRACK accounting policy.
    pub(crate) unsafe fn bind<'a, T, const TRACK: bool>(
        &'a mut self,
        store: &'a slots::Store<T>,
    ) -> Bound<'a, T, Tag, TRACK> {
        Bound { store, state: self }
    }
}

impl<T, Tag, const TRACK: bool> linked::Sealed<T> for Bound<'_, T, Tag, TRACK> {}

impl<T, Tag, const TRACK: bool> linked::Source<T> for Bound<'_, T, Tag, TRACK> {
    const TRACK: bool = TRACK;

    fn store(&self) -> &slots::Store<T> {
        self.store
    }
}

impl<'a, T, Tag> Bound<'a, T, Tag> {
    /// # Safety
    /// `index` is the unchanged front of this bound queue.
    pub(in crate::depot) unsafe fn detach(self, index: slots::Index<Tag>) -> Live<T, Self, Tag> {
        unsafe { self.state.unlink(self.store, index) };
        unsafe { Live::detached(self, index) }
    }

    pub fn pop_front(&mut self) -> Option<T> {
        let index = self.state.links.head()?;
        Some(unsafe { self.state.pop::<_, true>(self.store, index) })
    }

    pub fn front_entry(self) -> Option<slots::Front<'a, T, Tag>> {
        let index = self.state.links.head()?;
        Some(Front { owner: self, index })
    }
}

impl<'a, T, Tag, const TRACK: bool> slots::Vacant<T, Bound<'a, T, Tag, TRACK>, Tag> {
    pub fn append(self, value: T) {
        self.insert(value).publish::<false>();
    }
}

impl<T, Tag, const TRACK: bool> Live<T, Bound<'_, T, Tag, TRACK>, Tag> {
    pub(in crate::depot) fn publish<const FRONT: bool>(self) {
        let (owner, index) = unsafe { self.into_raw_parts() };
        // SAFETY: the bound owner retains this detached node's queue and store.
        unsafe { owner.state.link::<_, FRONT>(owner.store, index) };
    }
}

pub struct Front<'a, T, Tag> {
    owner: Bound<'a, T, Tag>,
    index: slots::Index<Tag>,
}

impl<T, Tag> Front<'_, T, Tag> {
    pub fn get_mut(&mut self) -> &mut T {
        // SAFETY: this entry's exclusive owner borrow is the capability for
        // the unchanged front slot; the returned borrow cannot outlive `self`.
        unsafe { &mut *self.owner.store.value_ptr(self.index) }
    }

    pub fn discard(self) {
        drop(unsafe { self.owner.detach(self.index) });
    }
}

const _: () = assert!(mem::size_of::<Front<'static, u64, ()>>() == 3 * mem::size_of::<usize>());