use std::marker;
use crate::depot::queue::round::{self, phase};
pub(super) mod front;
pub(super) mod place;
pub(super) mod turn;
#[derive(Clone, Copy, Debug)]
pub struct Snapshot<O, P> {
pub(in crate::depot::queue::round) owner: O,
pub(in crate::depot::queue::round) position: round::Position,
phase: marker::PhantomData<P>,
}
impl<O, P> Snapshot<O, P> {
pub(in crate::depot::queue::round) fn new(owner: O, position: round::Position) -> Self {
Self {
owner,
position,
phase: marker::PhantomData,
}
}
pub const fn index(&self) -> usize {
self.position.index.get()
}
pub const fn lane(&self) -> usize {
self.position.lane.get()
}
}
impl<'a, T, const G: usize, const N: usize, P> Snapshot<&'a round::Lanes<T, G, N>, P> {
fn resolve<const FAIR: bool>(
&self,
) -> Option<round::Location<&'a round::Lanes<T, G, N>, phase::Head>> {
let lane = unsafe { self.owner.join(self.position.lane) };
if lane.entries.first() != Some(self.position.index) {
return None;
}
let head = round::Location::new(self.owner, self.position);
(!FAIR || unsafe { lane.active.first_live() } == self.position.lane).then_some(head)
}
pub(in crate::depot::queue::round) fn filter<const FAIR: bool>(
&self,
test: impl FnOnce(round::Location<&'a round::Lanes<T, G, N>, phase::Head>) -> bool,
) -> Option<round::Location<&'a round::Lanes<T, G, N>, phase::Head>> {
if !test(self.resolve::<FAIR>()?) {
return None;
}
self.resolve::<FAIR>()
}
}
impl<O> From<round::Location<O, phase::Head>> for Snapshot<O, round::Front> {
fn from(head: round::Location<O, phase::Head>) -> Self {
Self::new(head.owner, head.position)
}
}
impl<O> From<round::Location<O, phase::Head>> for Snapshot<O, round::Turn> {
fn from(head: round::Location<O, phase::Head>) -> Self {
Self::new(head.owner, head.position)
}
}
impl<'a, T, const G: usize, const N: usize> crate::depot::raw::Admit
for crate::depot::raw::Proof<Snapshot<&'a round::Lanes<T, G, N>, round::Front>, phase::Ready>
{
type Output = Option<round::Location<&'a round::Lanes<T, G, N>, phase::Head>>;
fn admit(self) -> Self::Output {
self.into_inner().resolve::<false>()
}
}
impl<'a, T, const G: usize, const N: usize> crate::depot::raw::Admit
for crate::depot::raw::Proof<Snapshot<&'a round::Lanes<T, G, N>, round::Turn>, phase::Ready>
{
type Output = Option<
crate::depot::raw::Proof<
round::Location<&'a round::Lanes<T, G, N>, phase::Head>,
phase::Active,
>,
>;
fn admit(self) -> Self::Output {
let head = self.into_inner().resolve::<true>()?;
Some(unsafe { crate::depot::raw::Proof::new(head) })
}
}