use crate::depot::{
self,
arena::linked::{self, fifo, fifo::ends},
};
use std::{mem, ptr};
#[must_use]
#[repr(transparent)]
pub struct Live<T, H: linked::Source<T>, Tag = ()>(pub(super) linked::raw::Slot<T, H, Tag, true>);
impl<T, H: linked::Source<T>, Tag> Live<T, H, Tag> {
pub(crate) unsafe fn detached(owner: H, index: linked::raw::Index<Tag>) -> Self {
Self(unsafe { linked::raw::Slot::new(owner, index) })
}
pub(in crate::depot::arena::linked) unsafe fn claim<const BRANCH: u8>(
owner: H,
value: impl crate::mem::raw::Convert<T, BRANCH>,
) -> Self {
let (index, slot) = unsafe { owner.store().claim(Some(linked::raw::Index::NONE)) };
unsafe { (*slot).write(crate::mem::raw::Convert::<T, BRANCH>::into(value)) };
unsafe { Self::detached(owner, index) }
}
pub fn get(&self) -> &T {
unsafe { self.0.owner.store().value(self.0.index) }
}
pub(crate) fn take(self) -> (T, linked::raw::Vacant<T, H, Tag>) {
let (owner, index) = unsafe { self.0.into_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 {
linked::raw::Vacant::detached(owner, index)
})
}
pub fn append_ends(
proof: depot::raw::Proof<
(Self, &mut ends::Exclusive<linked::raw::Index<Tag>>),
fifo::Insert<false>,
>,
) {
let (node, ends) = proof.into_inner();
let (owner, index) = unsafe { node.0.into_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.0.into_parts() };
unsafe { index.link::<_, false>(queue, owner.store()) };
}
}
impl<T, M: Copy, Tag, const TRACK: bool> Live<(T, M), linked::raw::Borrow<'_, (T, M), TRACK>, Tag> {
pub fn take_into(self, place: &mut mem::MaybeUninit<T>) {
let (linked::raw::Borrow(store), index) = unsafe { self.0.into_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, linked::raw::Borrow<'_, T, TRACK>, Tag> {
pub unsafe fn recycle(self) {
let (linked::raw::Borrow(store), index) = unsafe { self.0.into_parts() };
let index = index.raw();
store.recycle(index, unsafe { store.slot(index) }, TRACK);
}
pub fn into_inner(self) -> T {
let (linked::raw::Borrow(store), index) = unsafe { self.0.into_parts() };
unsafe { store.take::<_, TRACK>(index) }
}
}
impl<T, Tag> Live<T, &linked::raw::Store<T>, Tag> {
pub(crate) unsafe fn into_index(self) -> linked::raw::Index<Tag> {
unsafe { self.0.into_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> Live<T, linked::raw::Bound<'_, T, Tag, TRACK>, Tag> {
pub fn publish<const FRONT: bool>(self) {
let (owner, index) = unsafe { self.0.into_parts() };
unsafe { index.link::<_, FRONT>(owner.state, owner.store) };
}
}