use std::mem;
use crate::depot::{arena::linked, capacity, queue::pool};
use pool::Front;
pub struct Lane<'a, T, S = (), const TRACK: bool = true> {
pub(in crate::depot::queue::pool) store: &'a linked::raw::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 linked::raw::Store<T>,
slot: &'a mut crate::depot::raw::QueueSlot<S>,
) -> Self {
Self { store, slot }
}
pub fn insert<I>(source: I) -> &'a mut S
where
T: Copy,
I: crate::depot::raw::PoolInsert<'a, TRACK, Item = T, State = S>,
{
let (lane, item) = unsafe { source.into_parts() };
let owner =
unsafe { linked::raw::Bound::<_, _, TRACK>::new(&mut lane.slot.queue, lane.store) };
unsafe { linked::raw::Vacant::copy_one(owner, item) }.publish::<false>();
&mut lane.slot.state
}
pub(super) unsafe fn insert_at<const FRONT: bool, const BRANCH: u8>(
self,
value: impl crate::mem::raw::Convert<T, BRANCH>,
) -> &'a mut S {
let owner =
unsafe { linked::raw::Bound::<_, _, TRACK>::new(&mut self.slot.queue, self.store) };
unsafe { linked::raw::Vacant::insert_one::<BRANCH>(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 vacant(&mut self) -> Option<linked::raw::Vacant<T, linked::raw::Bound<'_, T>>> {
let owner = unsafe { linked::raw::Bound::new(&mut self.slot.queue, self.store) };
unsafe { linked::raw::Vacant::try_reserve(owner) }
}
pub fn try_reserve(
self,
count: capacity::Count,
) -> Result<linked::raw::Reserve<T, linked::raw::Bound<'a, T>>, capacity::Shortage> {
self.store.check(count)?;
Ok(unsafe { self.reserve(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>());
};
unsafe impl<'a, T, S, R, const TRACK: bool> crate::depot::raw::Project<(), pool::Head>
for (Lane<'a, T, S, TRACK>, R)
{
type Output = (pool::Front<'a, T, S, TRACK>, R);
unsafe fn project(self, _: ()) -> Self::Output {
let (lane, retained) = self;
let index = lane.slot.queue.links.head_raw();
(pool::Front::new(lane, index), retained)
}
}
impl<'a, T, S> Lane<'a, T, S> {
pub unsafe fn reserve(
self,
count: capacity::Count,
) -> linked::raw::Reserve<T, linked::raw::Bound<'a, T>> {
let owner = unsafe { linked::raw::Bound::new(&mut self.slot.queue, self.store) };
unsafe { linked::raw::Reserve::new(owner, count) }
}
}
unsafe impl<'a, T, S, const TRACK: bool> crate::depot::raw::Project<linked::raw::Index>
for pool::View<'a, T, S, TRACK>
{
type Output = linked::Iter<'a, T>;
unsafe fn project(self, index: linked::raw::Index) -> Self::Output {
unsafe { linked::Iter::new(self.store, index) }
}
}