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(crate) 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(None) };
unsafe { Self::detached(owner, 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, slot) = unsafe { owner.store().claim(Some(slots::Index::NONE)) };
unsafe { (*slot).write(value) };
unsafe { Self::detached(owner, 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 fn append_ends(
proof: depot::raw::Proof<(Self, &mut ends::Exclusive<slots::Index<Tag>>), fifo::Append>,
) {
let (node, ends) = proof.into_inner();
let (owner, index) = unsafe { node.into_raw_parts() };
unsafe { ends.append_end(owner.store(), 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, M: Copy, Tag, const TRACK: bool> Live<(T, M), slots::Borrow<'_, (T, M), TRACK>, Tag> {
pub fn take_into(self, place: &mut mem::MaybeUninit<T>) {
let (slots::Borrow(store), index) = unsafe { self.into_raw_parts() };
let node = unsafe { store.slot(index.raw()) };
unsafe {
place
.as_mut_ptr()
.write(ptr::addr_of!((*node.value_ptr()).0).read());
}
store.recycle(index.raw(), node, TRACK);
}
}
impl<T, Tag, const TRACK: bool> Live<T, slots::Borrow<'_, T, TRACK>, Tag> {
pub fn into_inner(self) -> T {
let (slots::Borrow(store), index) = unsafe { self.into_raw_parts() };
unsafe { store.take::<_, TRACK>(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 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>();
}
}