use std::mem;
use crate::depot::{
arena::{
linked,
linked::{fifo::ends, slots},
},
raw::{Arena, Proof},
};
pub struct Fifo<'pool, T> {
pub(in crate::depot) arena: &'pool Arena<T>,
pub(in crate::depot) links: ends::Exclusive<slots::Index>,
}
impl<'pool, T> Fifo<'pool, T> {
pub(in crate::depot) const fn new(arena: &'pool Arena<T>) -> Self {
Self {
arena,
links: ends::Exclusive::empty(),
}
}
pub fn is_empty(&self) -> bool {
self.links.is_empty()
}
pub fn pop_front(&mut self) -> Option<T> {
let index = self.links.head()?;
let node =
Arena::fifo_detach(unsafe { Proof::new(((self.arena, &mut self.links), index)) });
Some(node.into_inner())
}
pub fn iter(&self) -> linked::Iter<'_, T> {
unsafe { self.arena.fifo_iter(&self.links) }
}
pub fn vacant(&mut self) -> Option<slots::Vacant<T, &mut Fifo<'pool, T>>> {
unsafe { slots::Vacant::try_reserve(self) }
}
}
impl<T> linked::Sealed<T> for &mut Fifo<'_, T> {}
impl<T> linked::Source<T> for &mut Fifo<'_, T> {
fn store(&self) -> &slots::Store<T> {
&self.arena.inner
}
}
impl<T> Drop for Fifo<'_, T> {
fn drop(&mut self) {
while self.pop_front().is_some() {}
}
}
impl<'fifo, 'pool, T> slots::Vacant<T, &'fifo mut Fifo<'pool, T>> {
pub fn push_back(self, value: T) {
let (fifo, index) = unsafe { self.insert(value).into_raw_parts() };
unsafe { fifo.links.append_end(&fifo.arena.inner, index) };
}
}
const _: () = {
let _: [(); mem::size_of::<Fifo<'static, u64>>()] = [(); 2 * mem::size_of::<usize>()];
let _: [(); mem::size_of::<slots::Vacant<u64, &'static mut Fifo<'static, u64>>>()] =
[(); 2 * mem::size_of::<usize>()];
};