use crate::depot::{self, queue::links};
pub unsafe trait Hooks<const PLANE: usize = 0> {
unsafe fn chain_push<C: crate::depot::raw::ChainCount>(
&self,
chain: &links::raw::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::raw::Chain<C>,
index: links::Index,
) -> links::Detached<'a> {
unsafe { chain.detach_hook::<false, PLANE, _>(self, index, self.hook_raw(index)) }.0
}
unsafe fn chain_pop<'a, C: crate::depot::raw::ChainCount>(
&'a self,
chain: &'a links::raw::Chain<C>,
index: links::Index,
) -> links::Detached<'a> {
unsafe { chain.detach_hook::<true, PLANE, _>(self, index, self.hook_raw(index)) }.0
}
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::raw::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::raw::List, index: links::Index, hook: &links::Hook) {
let adjacent = list.first().map(|next| {
(next, unsafe { self.hook_raw(next) }.into())
});
unsafe {
depot::raw::EndpointHook::from(hook).push::<true, _>(
list,
index,
links::Index::END,
adjacent,
)
};
}
unsafe fn unlink_raw(&self, list: &links::raw::List, hook: &links::Hook) {
unsafe { depot::raw::EndpointHook::from(hook).unlink::<PLANE, _, _>(list, self) };
}
}
unsafe impl<T> Hooks for [depot::raw::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 [depot::raw::StagedNode<K, Q, P>] {
unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
&unsafe { self.get_unchecked(index.get()) }.hook
}
}