use crate::alloc;
use crate::depot::{
arena::slab::{self, key::cycle},
capacity,
index::{Initialized, Unborrowed},
queue::links,
};
use crate::mem::Rejected;
use std::{array, cell, marker, mem, slice};
mod pinned;
mod store;
pub use pinned::Pinned;
pub use store::Store;
pub mod entries;
pub mod external;
pub mod key;
pub mod lookup;
pub mod raw;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Vacancy {
Full,
IdentityExhausted,
}
const _: () = {
assert!(size_of::<raw::Occupied<'_, u64>>() == 2 * size_of::<usize>());
assert!(size_of::<Option<raw::Occupied<'_, u64>>>() == 2 * size_of::<usize>());
assert!(size_of::<Option<slab::raw::Vacant<'_, u64>>>() == 2 * size_of::<usize>());
assert!(
size_of::<
Option<raw::Occupied<'_, u64, (), { u32::MAX }, entries::Exclusive, 1, true, true>>,
>() == 2 * size_of::<usize>()
);
assert!(
size_of::<raw::Occupied<'_, u64, (), { u32::MAX }, entries::Exclusive, 1, false>>()
== size_of::<raw::Occupied<'_, u64>>()
);
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum State<B, R> {
Free,
Reserved,
Occupied,
Busy(B),
Retired(R),
}
impl<B, R> State<B, R> {
fn classify<const MAX: u32, C>(
self,
actual: key::Generation<MAX>,
expected: key::Generation<MAX>,
) -> Result<(), key::Observation<MAX, B, R, C>> {
use key::Observation;
match self {
Self::Free => Err(Observation::Free { actual }),
Self::Reserved => Err(Observation::Reserved { actual }),
Self::Retired(retired) => Err(Observation::Retired { actual, retired }),
Self::Busy(borrow) => Err(Observation::Busy { actual, borrow }),
Self::Occupied if actual != expected => Err(Observation::Generation { actual }),
Self::Occupied => Ok(()),
}
}
}
pub enum Entry<'a, T, Tag = (), const MAX: u32 = { u32::MAX }> {
Vacant(slab::raw::Vacant<'a, T, Tag, MAX, entries::Exclusive, 1, false>),
Occupied(raw::Occupied<'a, T, Tag, MAX, entries::Exclusive, 1, false>),
}
impl<T, Tag, const MAX: u32> slab::Cell<T, Tag, MAX> {
pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
Ok(Self {
core: raw::Core::try_new(cap)?,
tag: marker::PhantomData,
})
}
pub fn capacity(&self) -> usize {
self.core.slots.capacity()
}
pub fn remaining(&self) -> capacity::Count {
self.core.slots.remaining()
}
pub fn len(&self) -> capacity::Count {
self.core.len()
}
pub fn is_empty(&self) -> bool {
self.len() == capacity::Count::EMPTY
}
pub fn is_full(&self) -> bool {
self.core.is_full()
}
pub fn vacancy(&self) -> Option<slab::raw::Vacant<'_, T, Tag, MAX, entries::Interior>> {
Some(slab::raw::Vacant::new(self.core.free()?))
}
pub fn try_vacancy(
&self,
) -> Result<slab::raw::Vacant<'_, T, Tag, MAX, entries::Interior>, slab::Vacancy> {
Ok(slab::raw::Vacant::new(self.core.try_free()?))
}
}
impl<'a, T, Tag, const MAX: u32> slab::Cell<T, Tag, MAX> {
pub fn entry(
&'a self,
parts: key::Parts<MAX>,
) -> Option<raw::Occupied<'a, T, Tag, MAX, entries::Interior>> {
let entry = slab::raw::Position::key(&self.core, parts.index(), parts.generation())?;
Some(raw::Occupied::new(entry.take()))
}
pub fn try_entry(&'a self, key: key::Handle<Tag, MAX>) -> Lookup<'a, T, Tag, MAX> {
Ok(raw::Occupied::new(
slab::raw::Position::try_key(&self.core, key)?.take(),
))
}
pub fn entry_at(
&'a self,
index: u32,
) -> Option<raw::Occupied<'a, T, Tag, MAX, entries::Interior>> {
let entry = slab::raw::Position::at(&self.core, index)?;
Some(raw::Occupied::new(entry.take()))
}
}
pub struct Cell<T, Tag = (), const MAX: u32 = { u32::MAX }> {
pub(in crate::depot) core: raw::Core<T, MAX, entries::Interior>,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<T, Tag, const MAX: u32> Cell<T, Tag, MAX> {
pub fn value_ptr<P>(
proof: crate::depot::raw::Proof<(&Self, links::Index), Initialized<P>>,
) -> *mut T {
let (owner, index) = proof.into_inner();
let slots = owner.core.slots.as_ref();
unsafe { slots.get_unchecked(index.get()).value_ptr() }
}
}
type Lookup<'a, T, Tag, const MAX: u32> = Result<
raw::Occupied<'a, T, Tag, MAX, entries::Interior>,
Rejected<key::Handle<Tag, MAX>, key::Observation<MAX, (), ()>>,
>;
impl<'a, T, Tag, const MAX: u32> slab::Cell<T, Tag, MAX> {
pub fn from_live<P>(
live: crate::depot::raw::Proof<(&'a Self, links::Index), Unborrowed<P>>,
) -> raw::Occupied<'a, T, Tag, MAX, entries::Interior> {
let (owner, index) = live.into_inner();
let entry = unsafe { raw::Position::new(&owner.core, index) };
raw::Occupied::new(entry.take())
}
}
pub struct Heap;
pub struct Inline<const N: usize>;
pub struct Lazy;
pub trait Storage: entries::Sealed {
const LAZY: bool = false;
type Plane<T, const MAX: u32, M: entries::Mode>: Plane<T, MAX, M>;
}
impl Storage for Heap {
type Plane<T, const MAX: u32, M: entries::Mode> = HeapEager<T, MAX, M>;
}
impl<const N: usize> Storage for Inline<N> {
type Plane<T, const MAX: u32, M: entries::Mode> = InlineEager<N, T, MAX, M>;
}
impl entries::Sealed for Heap {}
impl<const N: usize> entries::Sealed for Inline<N> {}
impl Storage for Lazy {
const LAZY: bool = true;
type Plane<T, const MAX: u32, M: entries::Mode> = HeapLazy<T, MAX, M>;
}
impl entries::Sealed for Lazy {}
type Slot<T, const MAX: u32, M> = crate::depot::raw::SlabSlot<
T,
<<M as entries::Mode>::Cycle as cycle::Policy>::Stored<MAX>,
<M as entries::Mode>::Busy,
<<M as entries::Mode>::Cycle as cycle::Policy>::Retired,
>;
type Shape<T, const MAX: u32, M> = crate::depot::raw::SlabCapacity<
T,
<<M as entries::Mode>::Cycle as cycle::Policy>::Stored<MAX>,
<M as entries::Mode>::Busy,
<<M as entries::Mode>::Cycle as cycle::Policy>::Retired,
>;
type Prepared<T, const MAX: u32, M> = crate::alloc::raw::Block<
Shape<T, MAX, M>,
cell::Cell<u32>,
(),
mem::MaybeUninit<Slot<T, MAX, M>>,
>;
type Ready<T, const MAX: u32, M> =
crate::alloc::raw::Block<Shape<T, MAX, M>, cell::Cell<u32>, (), Slot<T, MAX, M>>;
pub struct HeapEager<T, const MAX: u32, M: entries::Mode> {
block: Ready<T, MAX, M>,
}
impl<T, const MAX: u32, M: entries::Mode> HeapEager<T, MAX, M> {
fn remaining(&self) -> capacity::Count {
capacity::Count::new(self.count().raw().wrapping_sub(self.block.state.get()))
}
fn refusal(&self) -> Vacancy {
let retired = self.block.state.get();
if retired != 0 && retired == self.count().raw() {
Vacancy::IdentityExhausted
} else {
Vacancy::Full
}
}
}
impl<T, const MAX: u32, M: entries::Mode> HeapPlane<T, MAX, M> for HeapEager<T, MAX, M> {
fn try_alloc<const N: usize>(cap: capacity::Count) -> Result<Self, alloc::Error> {
const { assert!(!mem::needs_drop::<Slot<T, MAX, M>>()) };
let _ = const { key::Generation::<MAX>::MIN };
let epoch: <M::Cycle as cycle::Policy>::Stored<MAX> = Default::default();
let extent = crate::depot::raw::SlabCapacity::new(cap)?;
Ok(Self {
block: crate::alloc::raw::Uninit::try_from_fn(
extent,
cell::Cell::new(0),
|index| raw::Core::<T, MAX, M, N>::slot(index, cap.get(), epoch),
|_| cell::Cell::new(mem::MaybeUninit::uninit()),
)?,
})
}
}
impl<T, const MAX: u32, M: entries::Mode> Plane<T, MAX, M> for HeapEager<T, MAX, M> {
fn retired(&self) -> Option<&cell::Cell<u32>> {
Some(&self.block.state)
}
fn count(&self) -> capacity::Count {
self.block.extent().count
}
fn tail(&self) -> &[cell::Cell<mem::MaybeUninit<u32>>] {
self.block.tail()
}
fn as_ref(&self) -> &[Slot<T, MAX, M>] {
self.block.as_ref()
}
}
pub struct HeapLazy<T, const MAX: u32, M: entries::Mode> {
block: Prepared<T, MAX, M>,
built: cell::Cell<u32>,
}
impl<T, const MAX: u32, M: entries::Mode> HeapPlane<T, MAX, M> for HeapLazy<T, MAX, M> {
fn try_alloc<const N: usize>(cap: capacity::Count) -> Result<Self, alloc::Error> {
let _ = const { key::Generation::<MAX>::MIN };
let extent = crate::depot::raw::SlabCapacity::new(cap)?;
Ok(Self {
block: crate::alloc::raw::Uninit::try_fill(extent, cell::Cell::new(0), |_| {
(
mem::MaybeUninit::uninit(),
cell::Cell::new(mem::MaybeUninit::uninit()),
)
})?,
built: cell::Cell::new(0),
})
}
}
impl<T, const MAX: u32, M: entries::Mode> Plane<T, MAX, M> for HeapLazy<T, MAX, M> {
fn retired(&self) -> Option<&cell::Cell<u32>> {
Some(&self.block.state)
}
fn count(&self) -> capacity::Count {
self.block.extent().count
}
fn tail(&self) -> &[cell::Cell<mem::MaybeUninit<u32>>] {
self.block.tail()
}
fn as_ref(&self) -> &[Slot<T, MAX, M>] {
unsafe { slice::from_raw_parts(self.block.as_ptr().cast(), self.built() as usize) }
}
fn built(&self) -> u32 {
self.built.get()
}
fn push(&self, slot: Slot<T, MAX, M>) -> Option<links::Index> {
let index = self.built();
if index as usize >= self.capacity() {
return None;
}
unsafe { (&mut *self.block.as_ptr().add(index as usize)).write(slot) };
self.built.set(index + 1);
Some(unsafe { links::Index::new_raw(index) })
}
}
pub struct InlineEager<const N: usize, T, const MAX: u32, M: entries::Mode> {
slots: [Slot<T, MAX, M>; N],
cells: [cell::Cell<mem::MaybeUninit<u32>>; N],
}
impl<const N: usize, T, const MAX: u32, M: entries::Mode> InlineEager<N, T, MAX, M> {
pub(in crate::depot::arena::slab) fn inline<const PARTITIONS: usize>() -> Self {
const { assert!(N <= u32::MAX as usize, "inline slot count") };
let _ = const { key::Generation::<MAX>::MIN };
let epoch: <M::Cycle as cycle::Policy>::Stored<MAX> = Default::default();
Self {
slots: array::from_fn(|index| {
raw::Core::<T, MAX, M, PARTITIONS>::slot(index, N, epoch)
}),
cells: [const { cell::Cell::new(mem::MaybeUninit::uninit()) }; N],
}
}
}
impl<const N: usize, T, const MAX: u32, M: entries::Mode> Plane<T, MAX, M>
for InlineEager<N, T, MAX, M>
{
fn count(&self) -> capacity::Count {
capacity::Count::new(N as u32)
}
fn tail(&self) -> &[cell::Cell<mem::MaybeUninit<u32>>] {
&self.cells
}
fn as_ref(&self) -> &[Slot<T, MAX, M>] {
&self.slots
}
}
pub trait Plane<T, const MAX: u32, M: entries::Mode>: Sized + entries::Sealed {
fn count(&self) -> capacity::Count;
fn retired(&self) -> Option<&cell::Cell<u32>> {
None
}
fn tail(&self) -> &[cell::Cell<mem::MaybeUninit<u32>>];
fn as_ref(&self) -> &[Slot<T, MAX, M>];
fn built(&self) -> u32 {
self.count().raw()
}
fn push(&self, _: Slot<T, MAX, M>) -> Option<links::Index> {
None
}
fn capacity(&self) -> usize {
self.count().get()
}
fn get(&self, index: u32) -> Option<&Slot<T, MAX, M>> {
self.as_ref().get(index as usize)
}
}
pub trait HeapPlane<T, const MAX: u32, M: entries::Mode>: Plane<T, MAX, M> {
fn try_alloc<const N: usize>(cap: capacity::Count) -> Result<Self, alloc::Error>;
}
impl<T, const MAX: u32, M: entries::Mode> entries::Sealed for HeapEager<T, MAX, M> {}
impl<T, const MAX: u32, M: entries::Mode> entries::Sealed for HeapLazy<T, MAX, M> {}
impl<const N: usize, T, const MAX: u32, M: entries::Mode> entries::Sealed
for InlineEager<N, T, MAX, M>
{
}