use std::{mem, num::NonZero, ops, ptr};
use crate::depot::{
self,
arena::slab::key,
array::Index,
capacity,
queue::{
indexed::{
Live,
slab::{self, Slab, phase},
},
links, tagged,
},
};
#[must_use]
pub struct Slot<
'a,
T,
Tag = (),
const MAX: u32 = { u32::MAX },
H: depot::raw::IndexedHook = links::Hook,
const N: usize = 0,
C: depot::raw::Count = capacity::Count,
const LINKED: bool = true,
> {
pub(in crate::depot) core: &'a mut Slab<T, Tag, MAX, H, N, C>,
pub(in crate::depot) index: links::Index,
}
#[must_use]
pub struct Vacant<
'a,
T,
Tag = (),
const MAX: u32 = { u32::MAX },
H: depot::raw::IndexedHook = links::Hook,
const N: usize = 0,
C: depot::raw::Count = capacity::Count,
P: phase::State = phase::Vacant,
> {
pub(in crate::depot) core: &'a mut Slab<T, Tag, MAX, H, N, C>,
pub(in crate::depot) index: links::Index,
pub(in crate::depot) phase: P,
}
impl<
'a,
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
const LINKED: bool,
> Slot<'a, T, Tag, MAX, H, N, C, LINKED>
{
pub(in crate::depot) fn value(&self) -> *mut mem::MaybeUninit<T> {
unsafe { self.core.block.value(self.index) }
}
pub(in crate::depot) fn into_owner(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
(self.core, self.index)
}
}
impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
Slot<'a, T, Tag, MAX, H, N, C>
{
pub fn key(&self) -> key::Handle<Tag, MAX, true> {
unsafe { self.core.block.key(self.index) }
}
pub fn into_mut(self) -> &'a mut T {
unsafe { (&mut *self.value()).assume_init_mut() }
}
}
impl<
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
const LINKED: bool,
> AsRef<T> for Slot<'_, T, Tag, MAX, H, N, C, LINKED>
{
fn as_ref(&self) -> &T {
unsafe { (&*self.value()).assume_init_ref() }
}
}
impl<
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
const LINKED: bool,
> AsMut<T> for Slot<'_, T, Tag, MAX, H, N, C, LINKED>
{
fn as_mut(&mut self) -> &mut T {
unsafe { (&mut *self.value()).assume_init_mut() }
}
}
impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
Slot<'a, T, Tag, MAX, H, N, C, false>
{
fn leave(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
unsafe { self.core.block.state_mut() }.len -= 1;
self.into_owner()
}
pub(in crate::depot::queue) fn into_spare(
self,
) -> (T, Vacant<'a, T, Tag, MAX, H, N, C, phase::Issued>) {
Vacant::extract(self, phase::Issued)
}
pub(in crate::depot::queue) fn read(self) -> T {
let value = unsafe { self.value().cast::<T>().read() };
self.forget();
value
}
pub(in crate::depot::queue) fn forget(self) {
let (core, index) = self.leave();
unsafe { core.block.release::<phase::Issued>(index) };
}
pub(in crate::depot::queue) fn discard(self) {
unsafe { self.value().cast::<T>().drop_in_place() };
self.forget();
}
}
impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count> Slot<'a, T, Tag, MAX, links::Hook, 0, C> {
pub(in crate::depot::queue) fn detach(self) -> Slot<'a, T, Tag, MAX, links::Hook, 0, C, false> {
unsafe { depot::raw::Hooks::chain_unlink(&self.core.block, &self.core.chain, self.index) }
.idle();
Slot {
core: self.core,
index: self.index,
}
}
pub fn push_back(self) {
let hook = unsafe { depot::raw::Hooks::hook_raw(&self.core.block, self.index) };
unsafe {
self.core
.chain
.rotate(&self.core.block, self.index, hook, false)
};
}
pub fn remove(self) -> T {
unsafe { depot::raw::Hooks::chain_unlink(&self.core.block, &self.core.chain, self.index) };
Slot {
core: self.core,
index: self.index,
}
.read()
}
}
fn endpoint<const N: usize>(lane: Index<N>) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(u32::MAX - lane.index() as u32) }
}
impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C>
{
pub(in crate::depot::queue) fn detach(
self,
) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C, false> {
let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
unsafe { hook.unlink(&*self.core, self.index) };
Slot {
core: self.core,
index: self.index,
}
}
pub fn move_back(self, lane: Index<N>) {
let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
unsafe {
hook.bypass(&*self.core);
hook.push::<false, _>(&*self.core, endpoint(lane), self.index);
}
}
pub fn take(
self,
lane: Index<N>,
) -> (
T,
Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Restore<N>>,
) {
Vacant::extract(self.detach(), phase::Restore(lane))
}
pub fn remove(self) -> T {
let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
unsafe { hook.bypass(&*self.core) };
Slot {
core: self.core,
index: self.index,
}
.read()
}
}
impl<
'a,
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
P: phase::State,
> Vacant<'a, T, Tag, MAX, H, N, C, P>
{
fn extract(slot: Slot<'a, T, Tag, MAX, H, N, C, false>, phase: P) -> (T, Self) {
let value = unsafe { slot.value().cast::<T>().read() };
let (core, index) = slot.leave();
(value, Self { core, index, phase })
}
pub(in crate::depot) fn value(&self) -> *mut mem::MaybeUninit<T> {
unsafe { self.core.block.value(self.index) }
}
pub(in crate::depot) fn handle(&self) -> key::Handle<Tag, MAX, true> {
unsafe { self.core.block.key(self.index) }
}
pub(in crate::depot) fn into_owner(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
let this = mem::ManuallyDrop::new(self);
(unsafe { ptr::read(&this.core) }, this.index)
}
fn write(&mut self, value: T) {
unsafe { self.value().cast::<T>().write(value) };
}
unsafe fn published(self) -> Slot<'a, T, Tag, MAX, H, N, C> {
let (core, index) = self.into_owner();
unsafe { core.block.state_mut() }.len += 1;
Slot { core, index }
}
}
impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
Vacant<'a, T, Tag, MAX, H, N, C>
{
fn into_phase<Q: phase::State>(self, phase: Q) -> Vacant<'a, T, Tag, MAX, H, N, C, Q> {
let (core, index) = self.into_owner();
Vacant { core, index, phase }
}
}
impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count> Vacant<'a, T, Tag, MAX, links::Hook, 0, C> {
pub fn key(
self,
) -> (
key::Handle<Tag, MAX, true>,
Vacant<'a, T, Tag, MAX, links::Hook, 0, C, phase::Issued>,
) {
(self.handle(), self.into_phase(phase::Issued))
}
pub fn fill(self, value: T) {
let _ = self.insert(value);
}
}
impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count, P: phase::State>
Vacant<'a, T, Tag, MAX, links::Hook, 0, C, P>
{
pub fn insert(mut self, value: T) -> Slot<'a, T, Tag, MAX, links::Hook, 0, C> {
self.write(value);
unsafe {
depot::raw::Hooks::chain_push(&self.core.block, &self.core.chain, self.index);
self.published()
}
}
}
impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C>
{
pub fn select(
self,
lane: Index<N>,
) -> Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Target<N>> {
self.into_phase(phase::Target(lane))
}
}
impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count, P: phase::State>
Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, P>
{
unsafe fn publish<const FRONT: bool>(
self,
lane: Index<N>,
) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
unsafe {
hook.push::<FRONT, _>(&*self.core, endpoint(lane), self.index);
self.published()
}
}
}
impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Target<N>>
{
pub fn insert(mut self, value: T) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
self.write(value);
unsafe { self.assume_init() }
}
pub(in crate::depot) unsafe fn assume_init(
self,
) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
let lane = self.phase.0;
unsafe { self.publish::<false>(lane) }
}
}
impl<T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
Vacant<'_, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Restore<N>>
{
pub fn restore(mut self, value: T) {
self.write(value);
let lane = self.phase.0;
let _ = unsafe { self.publish::<true>(lane) };
}
}
impl<
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
P: phase::State,
> Drop for Vacant<'_, T, Tag, MAX, H, N, C, P>
{
fn drop(&mut self) {
unsafe { self.core.block.release::<P>(self.index) };
}
}
pub(super) fn resolve<
T,
Tag,
const MAX: u32,
H: depot::raw::IndexedHook,
const N: usize,
C: depot::raw::Count,
O,
>(
owner: O,
key: key::Handle<Tag, MAX, true>,
) -> Result<Live<O, links::Index, H>, slab::Error<Tag, MAX, C>>
where
O: ops::Deref<Target = Slab<T, Tag, MAX, H, N, C>>,
{
let core = &*owner;
let index = key.index();
let ready = core.block.state().initialized;
if index >= ready {
let capacity = core.capacity();
return Err(if index as usize >= capacity.count().get() {
slab::Error::Index { key, capacity }
} else {
slab::Error::Uninitialized {
key,
initialized: ready,
}
});
}
let meta = unsafe { core.block.metadata.add(index as usize).as_ref() };
let actual = meta.generation;
if !H::is_linked(&meta.hook, links::Index::from(key)) {
return Err(if unsafe { meta.hook.next() } == index {
slab::Error::Retired { key, actual }
} else {
slab::Error::Free { key, actual }
});
}
if actual != key.generation() {
return Err(slab::Error::Generation { key, actual });
}
Ok(unsafe { depot::raw::Proof::new((owner, links::Index::from(key))) })
}
pub(super) fn pop<T, Tag, const MAX: u32, C: depot::raw::Count>(
core: &mut Slab<T, Tag, MAX, links::Hook, 0, C>,
) -> Option<Slot<'_, T, Tag, MAX, links::Hook, 0, C, false>> {
let index = core.chain.first()?;
unsafe { depot::raw::Hooks::chain_pop(&core.block, &core.chain, index) };
Some(Slot { core, index })
}
const _: () = {
assert!(size_of::<Slot<'_, (), (), 1, tagged::raw::Hook, 2>>() == 2 * size_of::<usize>());
assert!(
size_of::<Option<Slot<'_, (), (), 1, tagged::raw::Hook, 2>>>() == 2 * size_of::<usize>()
);
assert!(
size_of::<Vacant<'_, (), (), 1, tagged::raw::Hook, 2, capacity::Count, phase::Restore<2>>>(
) == 2 * size_of::<usize>()
);
};