orx_selfref_col/references/
node_idx.rs1use super::NodePtr;
2use crate::{MemoryPolicy, MemoryState, Node, SelfRefCol, Variant};
3use core::fmt::Debug;
4use orx_pinned_vec::PinnedVec;
5
6pub struct NodeIdx<V: Variant> {
9 ptr: *mut Node<V>,
10 state: MemoryState,
11}
12
13impl<V: Variant> core::hash::Hash for NodeIdx<V> {
14 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
15 self.ptr.hash(state);
16 self.state.hash(state);
17 }
18}
19
20impl<V: Variant> Copy for NodeIdx<V> {}
22
23impl<V: Variant> Clone for NodeIdx<V> {
24 fn clone(&self) -> Self {
25 *self
26 }
27}
28
29impl<V: Variant> Debug for NodeIdx<V> {
30 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31 f.debug_struct("NodeIdx")
32 .field("ptr", &self.ptr)
33 .field("state", &self.state)
34 .finish()
35 }
36}
37
38impl<V: Variant> PartialEq for NodeIdx<V> {
39 fn eq(&self, other: &Self) -> bool {
40 self.ptr == other.ptr && self.state == other.state
41 }
42}
43
44impl<V: Variant> Eq for NodeIdx<V> {}
45
46impl<V> NodeIdx<V>
47where
48 V: Variant,
49{
50 #[inline(always)]
53 pub fn new(state: MemoryState, node_ptr: &NodePtr<V>) -> Self {
54 Self {
55 ptr: node_ptr.ptr_mut(),
56 state,
57 }
58 }
59
60 #[inline(always)]
62 pub fn is_in_state(&self, state: MemoryState) -> bool {
63 self.state == state
64 }
65
66 #[inline(always)]
67 pub(crate) fn ptr(&self) -> *const Node<V> {
68 self.ptr
69 }
70
71 #[inline(always)]
72 pub(crate) fn ptr_mut(&self) -> *mut Node<V> {
73 self.ptr
74 }
75
76 #[inline(always)]
79 pub fn get_ptr(&self, collection_state: MemoryState) -> Option<*mut Node<V>> {
80 self.state.eq(&collection_state).then_some(self.ptr)
81 }
82
83 #[inline(always)]
85 pub fn node_ptr(&self) -> NodePtr<V> {
86 NodePtr::new(self.ptr)
87 }
88
89 #[inline(always)]
96 pub fn is_valid_for<M, P>(&self, collection: &SelfRefCol<V, M, P>) -> bool
97 where
98 M: MemoryPolicy<V>,
99 P: PinnedVec<Node<V>>,
100 {
101 self.state == collection.memory_state()
102 && collection.nodes().contains_ptr(self.ptr)
103 && unsafe { &*self.ptr }.is_active()
104 }
105}