use std::mem;
use crate::{
depot::{arena::linked, index::Initialized, queue::pool},
mem::{Lease, lease},
};
pub struct Front<'a, T, S, const TRACK: bool = true> {
lane: pool::raw::Lane<'a, T, S, TRACK>,
index: linked::raw::Index,
}
impl<'a, T, S, const TRACK: bool> Front<'a, T, S, TRACK> {
pub fn from_source(
source: crate::depot::raw::Proof<
(pool::raw::Lane<'a, T, S, TRACK>, linked::raw::Index),
Initialized<pool::Head>,
>,
) -> Self {
let (lane, index) = source.into_inner();
Self { lane, index }
}
pub fn into_parts(self) -> (pool::raw::Lane<'a, T, S, TRACK>, linked::raw::Index) {
(self.lane, self.index)
}
pub(in crate::depot::queue::pool) const fn new(
lane: pool::raw::Lane<'a, T, S, TRACK>,
index: linked::raw::Index,
) -> Self {
Self { lane, index }
}
pub fn get(&self) -> &T {
unsafe { self.lane.store.value(self.index) }
}
pub fn get_mut(&mut self) -> &mut T {
self.parts_mut().0
}
pub fn parts_mut(&mut self) -> (&mut T, &mut S) {
let value = unsafe { &mut *self.lane.store.value_ptr(self.index) };
(value, &mut self.lane.slot.state)
}
pub fn take(self) -> (T, pool::raw::Lane<'a, T, S, TRACK>) {
let value = unsafe {
self.index
.pop::<_, TRACK>(&mut self.lane.slot.queue, self.lane.store)
};
(value, self.lane)
}
pub fn pop(self) -> T {
self.take().0
}
pub fn forget(self) -> pool::raw::Lane<'a, T, S, TRACK> {
unsafe {
self.index
.unlink(&mut self.lane.slot.queue, self.lane.store)
};
drop(unsafe {
linked::raw::Vacant::detached(
linked::raw::Borrow::<_, TRACK>(self.lane.store),
self.index,
)
});
self.lane
}
}
impl<'a, T, S> Front<'a, T, S> {
pub fn remove(self) -> Lease<linked::raw::Vacant<T, linked::raw::Bound<'a, T>>, lease::Open> {
let lane = self.lane;
let owner = unsafe { linked::raw::Bound::new(&mut lane.slot.queue, lane.store) };
let (value, claim) = unsafe { owner.detach(self.index) }.take();
Lease::open(claim, value)
}
}
const _: () = {
assert!(mem::size_of::<Front<'static, u64, ()>>() == 3 * mem::size_of::<usize>());
assert!(
mem::size_of::<Option<Front<'static, u64, ()>>>()
== mem::size_of::<Front<'static, u64, ()>>()
);
assert!(
mem::size_of::<(u64, pool::raw::Lane<'static, u64>)>()
== mem::size_of::<Front<'static, u64, ()>>()
);
};