use std::{marker, mem, ptr};
use crate::depot::{
self,
arena::linked::{self, fifo, fifo::ends, slots},
capacity,
};
pub type Vacant<T, H, Tag = ()> = Slot<T, H, Tag, false>;
pub type Live<T, H, Tag = ()> = Slot<T, H, Tag, true>;
#[must_use = "a detached slot retains its sole return entitlement"]
pub struct Slot<T, H: linked::Source<T>, Tag, const LIVE: bool> {
owner: H,
index: slots::Index<Tag>,
value: marker::PhantomData<T>,
}
impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Slot<T, H, Tag, LIVE> {
pub(in crate::depot) unsafe fn detached(owner: H, index: slots::Index<Tag>) -> Self {
Self {
owner,
index,
value: marker::PhantomData,
}
}
pub(crate) unsafe fn into_raw_parts(self) -> (H, slots::Index<Tag>) {
let slot = mem::ManuallyDrop::new(self);
(unsafe { ptr::read(&slot.owner) }, slot.index)
}
}
impl<T, H: linked::Source<T>, Tag, const LIVE: bool> Drop for Slot<T, H, Tag, LIVE> {
fn drop(&mut self) {
let store = self.owner.store();
let index = self.index.raw();
let node = unsafe { store.slot(index) };
if LIVE {
unsafe { (&mut *node.value.get()).assume_init_drop() };
}
store.recycle(index, node, H::TRACK);
}
}
impl<T, H: linked::Source<T>, Tag> Vacant<T, H, Tag> {
pub(in crate::depot) unsafe fn try_reserve(owner: H) -> Option<Self> {
if owner.store().available() == 0 {
return None;
}
Some(unsafe { Self::reserve(owner) })
}
fn debit(owner: &H) {
if H::TRACK {
owner.store().debit(capacity::Count::new(1));
}
}
pub(crate) unsafe fn reserve(owner: H) -> Self {
Self::debit(&owner);
unsafe { Self::claim(owner) }
}
pub(in crate::depot::arena::linked) unsafe fn claim(owner: H) -> Self {
let (index, _) = unsafe { owner.store().claim::<false>() };
unsafe { Self::detached(owner, slots::Index::new(index)) }
}
pub(crate) unsafe fn insert_one(owner: H, value: T) -> Live<T, H, Tag> {
Self::debit(&owner);
unsafe { Live::claim(owner, value) }
}
pub fn insert(self, value: T) -> Live<T, H, Tag> {
let (owner, index) = unsafe { self.into_raw_parts() };
let node = unsafe { owner.store().slot(index.raw()) };
unsafe { node.value.get().cast::<T>().write(value) };
node.next.set(slots::Store::<T>::END);
unsafe { Live::detached(owner, index) }
}
}
impl<T, H: linked::Source<T>, Tag> Live<T, H, Tag> {
pub(super) unsafe fn claim(owner: H, value: T) -> Self {
let (index, node) = unsafe { owner.store().claim::<true>() };
unsafe { node.value.get().cast::<T>().write(value) };
unsafe { Self::detached(owner, slots::Index::new(index)) }
}
pub fn get(&self) -> &T {
unsafe { self.owner.store().value(self.index) }
}
pub(crate) fn take(self) -> (T, Vacant<T, H, Tag>) {
let (owner, index) = unsafe { self.into_raw_parts() };
let node = unsafe { owner.store().slot(index.raw()) };
let value = unsafe { (&*node.value.get()).assume_init_read() };
node.next.set(index.raw());
(value, unsafe { Vacant::detached(owner, index) })
}
pub(in crate::depot) unsafe fn append_to(self, queue: &mut fifo::Fifo<Tag>) {
let (owner, index) = unsafe { self.into_raw_parts() };
unsafe { queue.link::<_, false>(owner.store(), index) };
}
}
impl<T, Tag> Live<T, &slots::Store<T>, Tag> {
pub(crate) unsafe fn into_index(self) -> slots::Index<Tag> {
unsafe { self.into_raw_parts() }.1
}
pub(crate) unsafe fn append(self, links: &mut ends::Exclusive<slots::Index<Tag>>) {
let (store, index) = unsafe { self.into_raw_parts() };
unsafe { links.append_end(store, index) };
}
pub fn push_back(proof: depot::raw::Proof<(Self, &mut fifo::Fifo<Tag>), linked::Same>) {
let (claim, queue) = proof.into_inner();
unsafe { claim.append_to(queue) };
}
}
impl<T, Tag, const TRACK: bool> crate::mem::Return
for Vacant<T, slots::Bound<'_, T, Tag, TRACK>, Tag>
{
type Item = T;
fn return_item(self, item: T) {
self.insert(item).publish::<true>();
}
}