use crate::{MemoryPolicy, Node, SelfRefCol, Variant};
use core::fmt::Debug;
use orx_pinned_vec::PinnedVec;
pub struct NodePtr<V: Variant> {
ptr: *mut Node<V>,
}
unsafe impl<V: Variant> Send for NodePtr<V> where V::Item: Send {}
unsafe impl<V: Variant> Sync for NodePtr<V> where V::Item: Sync {}
impl<V: Variant> PartialEq for NodePtr<V> {
fn eq(&self, other: &Self) -> bool {
self.ptr == other.ptr
}
}
impl<V: Variant> Debug for NodePtr<V> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("NodeIdx")
.field("ptr", &(self.ptr as usize))
.finish()
}
}
impl<V: Variant> Copy for NodePtr<V> {}
impl<V: Variant> Clone for NodePtr<V> {
fn clone(&self) -> Self {
*self
}
}
impl<V: Variant> NodePtr<V> {
pub fn new(ptr: *const Node<V>) -> Self {
Self {
ptr: ptr as *mut Node<V>,
}
}
#[inline(always)]
pub fn is_valid_for<M, P>(self, collection: &SelfRefCol<V, M, P>) -> bool
where
M: MemoryPolicy<V>,
P: PinnedVec<Node<V>>,
{
collection.nodes().contains_ptr(self.ptr) && unsafe { &*self.ptr }.is_active()
}
#[inline(always)]
pub unsafe fn ptr(self) -> *const Node<V> {
self.ptr
}
#[inline(always)]
pub unsafe fn ptr_mut(self) -> *mut Node<V> {
self.ptr
}
#[inline]
pub unsafe fn node<'a>(self) -> &'a Node<V> {
unsafe { &*self.ptr }
}
#[inline]
#[allow(clippy::mut_from_ref)]
pub unsafe fn node_mut<'a>(self) -> &'a mut Node<V> {
unsafe { &mut *self.ptr }
}
}