use std::mem;
use front::Front;
use crate::depot::{arena::linked::slots, capacity};
pub(super) mod front;
mod raw;
pub(super) mod view;
pub struct Lane<'a, T, S = (), const TRACK: bool = true> {
pub(in crate::depot::queue::pool) store: &'a slots::Store<T>,
pub(in crate::depot::queue::pool) slot: &'a mut crate::depot::raw::QueueSlot<S>,
}
impl<'a, T, S, const TRACK: bool> Lane<'a, T, S, TRACK> {
pub(in crate::depot) const fn new(
store: &'a slots::Store<T>,
slot: &'a mut crate::depot::raw::QueueSlot<S>,
) -> Self {
Self { store, slot }
}
pub fn insert<I>(source: I) -> &'a mut S
where
I: crate::depot::raw::PoolInsert<'a, TRACK, Item = T, State = S>,
{
let (lane, item) = unsafe { source.into_parts() };
unsafe { lane.insert_at::<false>(item) }
}
pub(super) unsafe fn insert_at<const FRONT: bool>(self, value: T) -> &'a mut S {
let owner = unsafe { self.slot.queue.bind::<_, TRACK>(self.store) };
unsafe { slots::Vacant::insert_one(owner, value) }.publish::<FRONT>();
&mut self.slot.state
}
pub const fn count(&self) -> capacity::Count {
self.slot.queue.count()
}
pub const fn is_empty(&self) -> bool {
self.slot.queue.is_empty()
}
pub const fn state(&self) -> &S {
&self.slot.state
}
pub const fn state_mut(&mut self) -> &mut S {
&mut self.slot.state
}
pub fn into_state(self) -> &'a mut S {
&mut self.slot.state
}
pub fn borrow_mut(&mut self) -> Lane<'_, T, S, TRACK> {
Lane::new(self.store, self.slot)
}
pub fn front_entry(self) -> Option<Front<'a, T, S, TRACK>> {
let index = self.slot.queue.links.head()?;
Some(Front::new(self, index))
}
pub fn front_mut(&mut self) -> Option<Front<'_, T, S, TRACK>> {
self.borrow_mut().front_entry()
}
pub fn pop_front(&mut self) -> Option<T> {
Some(self.front_mut()?.pop())
}
pub fn clear(&mut self) {
while self.pop_front().is_some() {}
}
}
impl<'a, T, S> Lane<'a, T, S> {
pub fn reserve(self) -> (Option<slots::Vacant<T, slots::Bound<'a, T>>>, &'a mut S) {
let owner = unsafe { self.slot.queue.bind(self.store) };
(
unsafe { slots::Vacant::try_reserve(owner) },
&mut self.slot.state,
)
}
pub fn vacant(&mut self) -> Option<slots::Vacant<T, slots::Bound<'_, T>>> {
self.borrow_mut().reserve().0
}
pub fn push_back(&mut self, value: T) -> Result<(), T> {
if self.store.available() == 0 {
return Err(value);
}
unsafe { self.borrow_mut().insert_at::<false>(value) };
Ok(())
}
pub fn try_reserve(
self,
count: capacity::Count,
) -> Result<slots::Reserve<T, slots::Bound<'a, T>>, capacity::Shortage> {
let owner = unsafe { self.slot.queue.bind(self.store) };
slots::Reserve::try_new(owner, count)
}
}
const _: () = {
assert!(mem::size_of::<Lane<'static, u64>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<crate::depot::raw::QueueSlot<()>>() == 3 * mem::size_of::<u32>());
};