use std::mem;
use crate::depot::{self, arena::linked::slots};
pub mod ends;
#[repr(C)]
pub struct Fifo<Tag = ()> {
pub(in crate::depot) links: ends::Exclusive<slots::Index<Tag>>,
len: u32,
}
impl<Tag> Default for Fifo<Tag> {
fn default() -> Self {
Self::new()
}
}
impl<Tag> Fifo<Tag> {
pub const fn new() -> Self {
Self {
links: ends::Exclusive::empty(),
len: 0,
}
}
pub const fn len(&self) -> usize {
self.len as usize
}
pub const fn is_empty(&self) -> bool {
self.len == 0
}
pub fn back(&self) -> Option<Back<Tag>> {
let index = self.links.tail_raw();
index.get()?;
Some(Back { index })
}
pub(in crate::depot) unsafe fn link<T, const FRONT: bool>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) {
if FRONT {
unsafe { self.links.prepend_link(store, index) };
} else {
unsafe { self.links.append_end(store, index) };
}
self.len = self.len.wrapping_add(1);
}
pub(in crate::depot::arena::linked) unsafe fn unlink<T>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) {
unsafe { self.links.unlink(store, index) };
self.len = self.len.wrapping_sub(1);
}
pub(in crate::depot) unsafe fn pop<T, const TRACK: bool>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) -> T {
unsafe { self.unlink(store, index) };
unsafe { store.take::<_, TRACK>(index) }
}
pub(in crate::depot) const fn count(&self) -> depot::capacity::Count {
depot::capacity::Count::new(self.len)
}
}
const _: () = assert!(mem::size_of::<Fifo>() == 3 * mem::size_of::<u32>());
#[must_use]
#[repr(transparent)]
pub struct Back<Tag> {
index: slots::Index<Tag>,
}
impl<Tag> Back<Tag> {
pub(in crate::depot) const fn into_index(self) -> slots::Index<Tag> {
self.index
}
}
const _: () = assert!(mem::size_of::<Back<()>>() == mem::size_of::<u32>());
#[derive(Debug)]
pub enum Detach {}
#[derive(Debug)]
pub enum Prepend {}
#[derive(Debug)]
pub enum Append {}
#[derive(Debug)]
pub enum Join {}
impl depot::raw::Sealed for Detach {}
impl depot::raw::Sealed for Prepend {}
impl depot::raw::Sealed for Append {}
impl depot::raw::Sealed for Join {}