use std::ops;
use crate::depot::queue::links;
pub unsafe trait Hooks<const PLANE: usize = 0> {
unsafe fn chain_push<C: crate::depot::raw::ChainCount>(
&self,
chain: &links::Chain<C>,
index: links::Index,
) {
unsafe { chain.link_back::<PLANE, _>(self, index, self.hook_raw(index)) };
}
unsafe fn chain_unlink<'a, C: crate::depot::raw::ChainCount>(
&'a self,
chain: &'a links::Chain<C>,
index: links::Index,
) -> links::Detached<'a> {
unsafe { chain.unlink::<PLANE, _>(self, index) }
}
unsafe fn chain_pop<'a, C: crate::depot::raw::ChainCount>(
&'a self,
chain: &'a links::Chain<C>,
index: links::Index,
) -> links::Detached<'a> {
unsafe { chain.pop::<PLANE, _>(self, index) }.0
}
unsafe fn cycle_step(&self, cycle: &links::Cycle, index: links::Index) -> links::Index {
unsafe { cycle.next_live::<PLANE, _>(self, index) }
}
unsafe fn cycle_push(&self, cycle: &links::Cycle, index: links::Index) {
unsafe { cycle.push::<PLANE, _>(self, index) };
}
unsafe fn cycle_unlink(&self, cycle: &links::Cycle, index: links::Index) {
unsafe { cycle.unlink_key::<PLANE, _>(self, index) };
}
unsafe fn acquire(&self, free: &links::Free) -> Option<links::Index> {
let index = free.front()?;
unsafe { free.acquire_at::<PLANE, _>(self, index) };
Some(index)
}
unsafe fn recycle(&self, free: &links::Free, index: links::Index) {
free.recycle(unsafe { links::Detached::new(self.hook_raw(index), index) });
}
unsafe fn push<C: crate::depot::raw::ChainCount>(
&self,
detached: links::Detached<'_>,
chain: &links::Chain<C>,
) {
unsafe { chain.push::<PLANE, _>(self, detached) };
}
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook;
unsafe fn link_raw(&self, list: &links::List, index: links::Index, hook: &links::Hook) {
let domain = &*self;
let adjacent = list.first().map(|next| {
(next, unsafe { domain.hook_raw(next) }.into())
});
unsafe {
EndpointHook::from(hook).push::<true, _>(list, index, links::Index::END, adjacent)
};
}
unsafe fn unlink_raw(&self, list: &links::List, hook: &links::Hook) {
let domain = &*self;
unsafe { EndpointHook::from(hook).unlink::<PLANE, _, _>(list, domain) };
}
}
#[derive(Debug)]
#[repr(C)]
pub struct Node<T> {
value: T,
hook: links::Hook,
}
impl<T> Node<T> {
pub fn vacant(value: T, index: u32, next: u32) -> Self {
Self {
value,
hook: links::Hook::vacant(index, next),
}
}
pub const fn hook(&self) -> &links::Hook {
&self.hook
}
}
impl<T> ops::Deref for Node<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
unsafe impl<T> Hooks for [Node<T>] {
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
unsafe { self.get_unchecked(index.get()) }.hook()
}
}
unsafe impl<K, Q, P> Hooks for [crate::depot::raw::StagedNode<K, Q, P>] {
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
&unsafe { self.get_unchecked(index.get()) }.hook
}
}
mod endpoint;
pub(in crate::depot) use endpoint::{EndpointHook, EndpointWord, Neighbor};
mod lanes;
pub(in crate::depot) use lanes::LaneStore;