o3 0.62.0

shared-nothing primitives
Documentation
use std::{marker, ops, pin::Pin};

pub use grant::Grant;
pub use occupied::Occupied;
pub use staged::Staged;
pub use vacant::Vacant;

use crate::{
    base::num,
    depot::{
        self,
        arena::slab::{self, Plane, entries, external},
        capacity,
        index::Initialized,
    },
};

pub mod errors;
mod grant;
mod occupied;
pub mod raw;
pub mod shared;
mod staged;
mod vacant;

type Allocated<'a, T, G, Tag> =
    depot::raw::Proof<(&'a shared::Storage<T, G, Tag>, u32), Initialized<entries::Allocated>>;

/// A typed external identity independent of the slab's private generation.
#[repr(C)]
#[derive(Debug)]
pub struct Key<G: num::Step, Tag = ()> {
    generation: G,
    index: u32,
    tag: marker::PhantomData<*mut Tag>,
}

pub struct Stamped<T, G: num::Step> {
    pub(in crate::depot) generation: G,
    pub(in crate::depot) value: T,
}

/// Exclusive storage with non-wrapping logical identities.
pub struct Exclusive<T, G: num::Step, Tag = (), const PARTITIONS: usize = 1> {
    pub(in crate::depot) core:
        slab::raw::Core<Stamped<T, G>, 1, entries::Exclusive<true>, PARTITIONS>,
    tag: marker::PhantomData<fn() -> Tag>,
    next: Option<G>,
}

impl<G: num::Step, Tag> Key<G, Tag> {
    #[must_use]
    pub const fn new(index: u32, epoch: G) -> Self {
        Self {
            generation: epoch,
            index,
            tag: marker::PhantomData,
        }
    }

    #[must_use]
    pub const fn index(self) -> u32 {
        self.index
    }

    #[must_use]
    pub const fn generation(self) -> G {
        self.generation
    }
}

impl<G: num::Step, Tag> Clone for Key<G, Tag> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<G: num::Step, Tag> Copy for Key<G, Tag> {}

impl<G: num::Step, Tag> PartialEq for Key<G, Tag> {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index && self.generation == other.generation
    }
}

impl<G: num::Step, Tag> Eq for Key<G, Tag> {}

impl<T, G: num::Step, Tag, const PARTITIONS: usize> Exclusive<T, G, Tag, PARTITIONS> {
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: slab::raw::Core::try_new(cap)?,
            tag: marker::PhantomData,
            next: Some(G::INITIAL),
        })
    }
}

impl<T, Tag, const PARTITIONS: usize> Exclusive<T, num::Epoch, Tag, PARTITIONS> {
    pub const fn next(&self) -> Option<num::Epoch> {
        self.next
    }

    /// Raises the issuance floor; either exhausted domain remains exhausted.
    pub fn advance_to(&mut self, floor: Option<num::Epoch>) {
        self.next = self
            .next
            .zip(floor)
            .map(|(current, floor)| current.max(floor));
    }
}

impl<'a, T, G: num::Step, Tag, const PARTITIONS: usize> Exclusive<T, G, Tag, PARTITIONS> {
    pub fn capacity(&self) -> capacity::Count {
        self.core.slots.count()
    }

    pub fn len(&self) -> capacity::Count {
        self.core.len()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.core.values_mut().map(|stamp| &mut stamp.value)
    }

    pub fn get(&'a self, key: Key<G, Tag>) -> Option<&'a T> {
        let stamp = slab::raw::Position::at(&self.core, key.index())?.read();
        (stamp.generation == key.generation()).then_some(&stamp.value)
    }

    pub fn key_at(&'a self, index: u32) -> Option<Key<G, Tag>> {
        let stamp = slab::raw::Position::at(&self.core, index)?.read();
        Some(Key::new(index, stamp.generation))
    }
}

impl<'a, T, G: num::Step, Tag, const PARTITIONS: usize> Exclusive<T, G, Tag, PARTITIONS> {
    /// Validates an identity once and retains exclusive access to its live slot.
    pub fn try_occupied(
        &'a mut self,
        key: Key<G, Tag>,
    ) -> Result<Occupied<'a, T, G, Tag, entries::Exclusive<true>, PARTITIONS>, errors::Lookup<G, Tag>>
    {
        Ok(Occupied::new(
            slab::raw::Position::stamped(
                key,
                slab::raw::Position::try_at(&self.core, key.index()),
            )?
            .take(),
        ))
    }

    pub fn occupied(
        &'a mut self,
        key: Key<G, Tag>,
    ) -> Option<Occupied<'a, T, G, Tag, entries::Exclusive<true>, PARTITIONS>> {
        let entry = slab::raw::Position::at(&self.core, key.index())?.take();
        (entry.value().generation == key.generation()).then_some(Occupied::new(entry))
    }

    /// Borrows the current occupied owner at one physical position.
    pub fn occupied_at(
        &'a mut self,
        index: u32,
    ) -> Option<Occupied<'a, T, G, Tag, entries::Exclusive<true>, PARTITIONS>> {
        Some(Occupied::new(
            slab::raw::Position::at(&self.core, index)?.take(),
        ))
    }
}

impl<'a, T, G: num::Step, Tag, const N: usize> Exclusive<T, G, Tag, N> {
    pub fn try_grant(&'a mut self) -> Result<Grant<'a, T, G, Tag, N>, slab::Vacancy> {
        let epoch = self.next.ok_or(slab::Vacancy::IdentityExhausted)?;
        let entry = self.core.free().ok_or(slab::Vacancy::Full)?;
        Ok(Grant {
            entry,
            next: &mut self.next,
            generation: epoch,
            tag: marker::PhantomData,
        })
    }

    pub fn try_grant_in(
        &'a mut self,
        range: ops::Range<u32>,
    ) -> Result<Grant<'a, T, G, Tag, N>, slab::Vacancy> {
        let epoch = self.next.ok_or(slab::Vacancy::IdentityExhausted)?;
        let entry = self.core.in_range(range).ok_or(slab::Vacancy::Full)?;
        Ok(Grant {
            entry,
            next: &mut self.next,
            generation: epoch,
            tag: marker::PhantomData,
        })
    }

    /// Physical absence precedes logical exhaustion; failure spends neither identity nor slot.
    pub fn try_except(
        &'a mut self,
        range: ops::Range<u32>,
        excluded: u32,
    ) -> Result<Grant<'a, T, G, Tag, N>, slab::Vacancy> {
        let free = slab::raw::Free::in_range(&mut self.core, range, excluded)
            .ok_or(slab::Vacancy::Full)?;
        let epoch = self.next.ok_or(slab::Vacancy::IdentityExhausted)?;
        Ok(Grant {
            entry: free.take(),
            next: &mut self.next,
            generation: epoch,
            tag: marker::PhantomData,
        })
    }
}

pub struct Pinned<T, G: num::Step, Tag = ()> {
    pub(in crate::depot) core:
        slab::raw::Core<Stamped<T, G>, 1, entries::Exclusive<true>, 1, slab::Lazy>,
    next: Option<G>,
    tag: marker::PhantomData<fn() -> Tag>,
    _pin: marker::PhantomPinned,
}

impl<T, G: num::Step, Tag> Pinned<T, G, Tag> {
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: slab::raw::Core::try_new(cap)?,
            tag: marker::PhantomData,
            _pin: marker::PhantomPinned,
            next: Some(G::INITIAL),
        })
    }

    pub fn capacity(&self) -> capacity::Count {
        self.core.slots.count()
    }

    pub fn try_grant(
        self: Pin<&mut Self>,
    ) -> Result<external::raw::Anchor<'_, T, G, Tag>, slab::Vacancy> {
        raw::Anchor::try_new(self)
    }

    pub fn try_occupied(
        self: Pin<&mut Self>,
        key: external::Key<G, Tag>,
    ) -> Result<external::raw::Anchored<'_, T, G, Tag>, errors::Lookup<G, Tag>> {
        raw::Anchored::try_new(self, key)
    }
}