use std::mem;
pub use error::Error;
pub use reserved::Reserved;
use crate::depot::{
arena::slab::key,
array::Index,
capacity,
queue::{indexed::slab, links, tagged},
};
mod error;
pub mod phase;
pub mod raw;
mod reserved;
pub struct Slab<
T,
Tag = (),
const MAX: u32 = { u32::MAX },
H: crate::depot::raw::IndexedHook = links::Hook,
const N: usize = 0,
C: crate::depot::raw::Count = capacity::Count,
> {
pub(in crate::depot) block: crate::depot::raw::IndexedBlock<T, Tag, MAX, H, N, C>,
chain: H::Chain,
}
impl<
T,
Tag,
const MAX: u32,
H: crate::depot::raw::IndexedHook,
const N: usize,
C: crate::depot::raw::Count,
> Slab<T, Tag, MAX, H, N, C>
{
pub fn try_from(capacity: C) -> Result<Self, crate::alloc::Error> {
const { assert!(N <= u8::MAX as usize) };
let shape = tagged::Domain::<[H::Head; N], C>::new(capacity);
Ok(Self {
block: crate::depot::raw::IndexedBlock::try_new(shape)?,
chain: H::Chain::default(),
})
}
pub fn capacity(&self) -> C {
self.block.shape.capacity()
}
pub fn len(&self) -> usize {
self.block.state().len as usize
}
pub fn available(&self) -> usize {
self.block.state().available as usize
}
pub fn vacant(&mut self) -> Option<raw::Vacant<'_, T, Tag, MAX, H, N, C>> {
let (core, [index]) = Reserved::admit(self)?.into_parts();
Some(raw::Vacant {
core,
index,
phase: phase::Vacant,
})
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn is_full(&self) -> bool {
self.available() == 0
}
pub fn try_get(
&self,
handle: key::Handle<Tag, MAX, true>,
) -> Result<&T, slab::Error<Tag, MAX, C>> {
Ok(raw::resolve(self, handle)?.into())
}
pub fn try_entry(
&mut self,
key: key::Handle<Tag, MAX, true>,
) -> Result<raw::Slot<'_, T, Tag, MAX, H, N, C>, slab::Error<Tag, MAX, C>> {
Ok(raw::resolve(self, key)?.into())
}
}
impl<T, Tag, const MAX: u32, C: crate::depot::raw::Count> Slab<T, Tag, MAX, links::Hook, 0, C> {
pub fn entry(
&mut self,
key: key::Handle<Tag, MAX, true>,
) -> Option<raw::Slot<'_, T, Tag, MAX, links::Hook, 0, C>> {
Some(raw::resolve(self, key).ok()?.into())
}
pub fn front_pair(&self) -> Option<(key::Handle<Tag, MAX, true>, &T)> {
let index = self.chain.first()?;
Some(unsafe { self.block.pair(index) })
}
}
impl<T, Tag, const MAX: u32, const N: usize, C: crate::depot::raw::Count>
Slab<T, Tag, MAX, tagged::raw::Hook, N, C>
{
pub fn select<I, const M: usize>(
&mut self,
order: [(Index<N>, I); M],
) -> Option<(I, raw::Slot<'_, T, Tag, MAX, tagged::raw::Hook, N, C>)> {
for (lane, item) in order {
let head = lane.get(self.block.heads());
if let Some(index) = head.first() {
return Some((item, raw::Slot { core: self, index }));
}
}
None
}
pub fn front_pair(&self, lane: Index<N>) -> Option<(key::Handle<Tag, MAX, true>, &T)> {
let head = lane.get(self.block.heads());
let index = head.first()?;
Some(unsafe { self.block.pair(index) })
}
}
impl<T, Tag, const MAX: u32, C: crate::depot::raw::Count> Slab<T, Tag, MAX, links::Hook, 0, C> {
pub fn pop_front(&mut self) -> Option<T> {
Some(raw::pop(self)?.read())
}
}
const _: () = {
assert!(mem::size_of::<crate::depot::raw::IndexedState>() == 4 * mem::size_of::<u32>());
assert!(
mem::size_of::<crate::depot::raw::IndexedMeta<{ u32::MAX }, links::Hook>>()
== 3 * mem::size_of::<u32>()
);
assert!(
mem::align_of::<crate::depot::raw::IndexedMeta<{ u32::MAX }, links::Hook>>()
== mem::align_of::<links::raw::Chain<()>>()
);
assert!(mem::size_of::<Slab<u64, (), { u32::MAX }>>() == 4 * mem::size_of::<usize>());
};