use crate::base::num;
use crate::depot::arena::slab::external::errors;
use std::{convert, marker};
use crate::depot::{
arena::slab::{self, Plane, entries, external, lookup},
index::Initialized,
queue::links,
};
type Live<S, P> = crate::depot::raw::Proof<(S, links::Index), Initialized<P>>;
pub struct Occupied<
'a,
T,
G: num::Step,
Tag = (),
M: entries::Mode = entries::Exclusive<true>,
const N: usize = 1,
> {
pub(super) entry:
slab::raw::Entry<'a, external::Stamped<T, G>, 1, M, N, slab::Heap, true, false>,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<'a, T, G: num::Step, Tag, M: entries::Mode, const N: usize> Occupied<'a, T, G, Tag, M, N> {
pub(in crate::depot::arena::slab) fn new(
entry: slab::raw::Entry<'a, external::Stamped<T, G>, 1, M, N, slab::Heap, true, false>,
) -> Self {
Self {
entry,
tag: marker::PhantomData,
}
}
pub fn key(&self) -> external::Key<G, Tag> {
external::Key::new(self.entry.index().raw(), self.entry.value().generation)
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.entry.value_mut().value
}
pub fn remove(self) -> T {
self.entry.remove().value
}
pub fn discard(self) {
self.entry.issue();
}
}
impl<'a, T, G: num::Step, Tag, const N: usize>
Occupied<'a, T, G, Tag, entries::Exclusive<true>, N>
{
pub fn from_live<P>(live: Live<&'a mut external::Exclusive<T, G, Tag, N>, P>) -> Self {
let (owner, index) = live.into_inner();
Self::new(unsafe { owner.core.project(index) })
}
pub fn is_full(&self) -> bool {
self.entry.core().len() == self.entry.core().slots.count()
}
pub fn get(&self) -> &T {
&self.entry.value().value
}
pub fn into_mut(self) -> &'a mut T {
&mut self.entry.into_mut().value
}
pub fn project<P>(live: Live<&Self, P>) -> &T {
let (owner, index) = live.into_inner();
&unsafe { owner.entry.core().read(index) }.value
}
pub fn retire(self) -> external::raw::Withdrawn<'a, T, G, N, true> {
external::raw::Withdrawn::new(self)
}
}
impl<'a, T, G: num::Step, Tag> Occupied<'a, T, G, Tag, entries::Interior<true>> {
pub fn forget(self) {
unsafe { self.entry.release() };
}
pub fn allocated(
proof: external::Allocated<'a, T, G, Tag>,
) -> Result<Self, lookup::Error<(), convert::Infallible>> {
let (owner, index) = proof.into_inner();
Ok(Self::new(
unsafe { slab::raw::Position::allocated(&owner.core, index) }?.take(),
))
}
pub fn try_bound(
proof: external::Allocated<'a, T, G, Tag>,
epoch: G,
) -> Result<Self, errors::Lookup<G, Tag, ()>> {
let (owner, index) = proof.into_inner();
let key = external::Key::new(index, epoch);
let live = unsafe { slab::raw::Position::allocated(&owner.core, index) };
Ok(Self::new(slab::raw::Position::stamped(key, live)?.take()))
}
}