use std::mem;
use crate::depot::arena::linked::{head, slots};
#[must_use]
pub struct Entry<'a, 'b, T, Tag> {
store: &'a slots::Store<T>,
state: &'b mut head::List<Tag>,
}
impl<'a, 'b, T, Tag> Entry<'a, 'b, T, Tag> {
pub(in crate::depot::arena::linked) const unsafe fn new(
store: &'a slots::Store<T>,
state: &'b mut head::List<Tag>,
) -> Self {
Self { store, state }
}
pub fn get(&self) -> &T {
unsafe { self.store.value(self.state.head) }
}
pub fn pop(self) -> T {
self.take().0
}
pub fn take(self) -> (T, slots::Vacant<T, &'a slots::Store<T>, Tag>) {
let index = self.state.head;
self.state.head = unsafe { self.store.next(index) };
unsafe { self.store.detached(index) }.take()
}
}
const _: () =
assert!(mem::size_of::<Entry<'static, 'static, u64, ()>>() == 2 * mem::size_of::<usize>());
pub(crate) struct Drain<'a, T, Tag = ()> {
store: &'a slots::Store<T>,
next: slots::Index<Tag>,
}
impl<'a, T, Tag> Drain<'a, T, Tag> {
pub(crate) unsafe fn new(store: &'a slots::Store<T>, next: slots::Index<Tag>) -> Self {
Self { store, next }
}
}
impl<'a, T, Tag> Iterator for Drain<'a, T, Tag> {
type Item = slots::Live<T, &'a slots::Store<T>, Tag>;
fn next(&mut self) -> Option<Self::Item> {
let head = self.next;
head.get()?;
self.next = unsafe { self.store.next(head) };
Some(unsafe { self.store.detached(head) })
}
}
impl<T, Tag> Drop for Drain<'_, T, Tag> {
fn drop(&mut self) {
for _ in self {}
}
}