use std::{
mem::{self, ManuallyDrop},
ops::Deref,
ptr,
};
use crate::depot::{
arena::slab::{
self, Plane, entries,
key::{self, cycle, cycle::Policy},
},
queue::links,
};
pub(in crate::depot) struct Entry<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
S: slab::Storage,
const LIVE: bool,
const ISSUED: bool,
> {
pub(super) position: slab::raw::Position<'a, T, MAX, M, N, S, LIVE>,
}
impl<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
S: slab::Storage,
const LIVE: bool,
const ISSUED: bool,
> Deref for Entry<'a, T, MAX, M, N, S, LIVE, ISSUED>
{
type Target = slab::raw::Position<'a, T, MAX, M, N, S, LIVE>;
fn deref(&self) -> &Self::Target {
&self.position
}
}
impl<
'a,
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
S: slab::Storage,
const LIVE: bool,
const ISSUED: bool,
> Entry<'a, T, MAX, M, N, S, LIVE, ISSUED>
{
pub(in crate::depot::arena::slab) unsafe fn new(
core: &'a slab::raw::Core<T, MAX, M, N, S>,
index: links::Index,
) -> Self {
Self {
position: unsafe { slab::raw::Position::new(core, index) },
}
}
pub(in crate::depot::arena::slab) fn into_place(
self,
) -> slab::raw::Position<'a, T, MAX, M, N, S, LIVE> {
let held = ManuallyDrop::new(self);
unsafe { ptr::read(&held.position) }
}
fn phase<const NEXT_LIVE: bool, const NEXT_ISSUED: bool>(
self,
) -> Entry<'a, T, MAX, M, N, S, NEXT_LIVE, NEXT_ISSUED> {
let prior = self.into_place();
unsafe { Entry::new(prior.core(), prior.index()) }
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const ISSUED: bool>
Entry<'a, T, MAX, M, N, S, true, ISSUED>
{
pub(in crate::depot) fn value(&self) -> &T {
unsafe { self.slot().value() }
}
pub(in crate::depot) fn value_mut(&mut self) -> &mut T {
unsafe { &mut *self.slot().value_ptr() }
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
Entry<'a, T, MAX, M, N, S, true, false>
{
pub(in crate::depot::arena::slab) fn withdraw(self) -> Entry<'a, T, MAX, M, N, S, false, true> {
let member = slab::raw::Retirement::new(&self.position);
unsafe { member.unlink() };
member.slot.metadata.state.set(slab::State::Reserved);
self.phase()
}
pub(in crate::depot) fn remove(self) -> T {
let member = slab::raw::Retirement::new(&self.into_place());
let value = unsafe { member.slot.take_value() };
unsafe { member.recycle::<true>() };
value
}
pub(in crate::depot::arena::slab) unsafe fn release(self) {
unsafe { slab::raw::Retirement::new(&self.into_place()).recycle::<true>() };
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
Entry<'a, T, MAX, M, N, S, true, true>
{
pub(in crate::depot::arena::slab) fn commit(self) -> Entry<'a, T, MAX, M, N, S, true, false> {
Entry {
position: self.into_place(),
}
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const LIVE: bool>
Entry<'a, T, MAX, M, N, S, LIVE, false>
{
pub(in crate::depot) fn issue(self) -> Entry<'a, T, MAX, M, N, S, LIVE, true> {
Entry {
position: self.into_place(),
}
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage>
Entry<'a, T, MAX, M, N, S, false, false>
{
pub(in crate::depot::arena::slab) unsafe fn publish_key<Tag>(
self,
) -> (
key::Handle<Tag, MAX>,
Entry<'a, T, MAX, M, N, S, true, false>,
) {
let key = key::Handle::new(self.index().raw(), self.generation());
(key, unsafe { self.publish() })
}
}
impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, S: slab::Storage, const ISSUED: bool>
Entry<'a, T, MAX, M, N, S, false, ISSUED>
{
pub(in crate::depot) fn uninit(&mut self) -> &mut mem::MaybeUninit<T> {
unsafe { &mut *self.slot().value_ptr().cast::<mem::MaybeUninit<T>>() }
}
pub(in crate::depot) fn install<const STAGED: bool>(
mut self,
value: T,
) -> Entry<'a, T, MAX, M, N, S, true, STAGED> {
self.uninit().write(value);
unsafe { self.publish() }
}
pub(in crate::depot::arena::slab) unsafe fn publish<const STAGED: bool>(
self,
) -> Entry<'a, T, MAX, M, N, S, true, STAGED> {
let pos = self.core().live.get();
let slot = self.slot();
unsafe {
self.core()
.slots
.tail()
.get_unchecked(pos as usize)
.set(mem::MaybeUninit::new(self.index().raw()))
};
self.core().live.set(pos + 1);
slot.metadata.position.set(pos);
slot.metadata.state.set(M::TAKEN);
self.phase()
}
}
impl<'a, T, const MAX: u32, const RECYCLE: bool, const N: usize, S: slab::Storage>
Entry<'a, T, MAX, entries::Exclusive<RECYCLE>, N, S, true, false>
where
cycle::Mode<RECYCLE>: Policy,
{
pub(in crate::depot) fn into_mut(self) -> &'a mut T {
unsafe { &mut *self.slot().value_ptr() }
}
}
impl<
T,
const MAX: u32,
M: entries::Mode,
const N: usize,
S: slab::Storage,
const LIVE: bool,
const ISSUED: bool,
> Drop for Entry<'_, T, MAX, M, N, S, LIVE, ISSUED>
{
fn drop(&mut self) {
if !ISSUED && LIVE {
if M::REENTRANT {
self.slot().metadata.state.set(slab::State::Occupied);
}
return;
}
let member = slab::raw::Retirement::new(&self.position);
if ISSUED && LIVE {
unsafe { member.slot.drop_value() };
}
unsafe { member.recycle::<ISSUED>() };
}
}
impl<'a, T, const MAX: u32, const N: usize, S: slab::Storage>
Entry<'a, T, MAX, entries::Exclusive, N, S, true, false>
{
pub(in crate::depot) fn replace(&mut self, next: key::Generation<MAX>, value: T) -> T {
let prior = mem::replace(self.value_mut(), value);
self.slot().metadata.generation.set(next);
prior
}
}