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
6#[derive(Copy)]
9pub struct NodeIdx<V: Variant> {
10 ptr: *mut Node<V>,
11 state: MemoryState,
12}
13
14impl<V: Variant> Clone for NodeIdx<V> {
15 fn clone(&self) -> Self {
16 Self {
17 ptr: self.ptr,
18 state: self.state,
19 }
20 }
21}
22
23impl<V: Variant> Debug for NodeIdx<V> {
24 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25 f.debug_struct("NodeIdx")
26 .field("ptr", &self.ptr)
27 .field("state", &self.state)
28 .finish()
29 }
30}
31
32impl<V: Variant> PartialEq for NodeIdx<V> {
33 fn eq(&self, other: &Self) -> bool {
34 self.ptr == other.ptr && self.state == other.state
35 }
36}
37
38impl<V: Variant> Eq for NodeIdx<V> {}
39
40impl<V> NodeIdx<V>
41where
42 V: Variant,
43{
44 #[inline(always)]
47 pub fn new(state: MemoryState, node_ptr: &NodePtr<V>) -> Self {
48 Self {
49 ptr: node_ptr.ptr_mut(),
50 state,
51 }
52 }
53
54 #[inline(always)]
56 pub fn is_in_state(&self, state: MemoryState) -> bool {
57 self.state == state
58 }
59
60 #[inline(always)]
61 pub(crate) fn ptr(&self) -> *const Node<V> {
62 self.ptr
63 }
64
65 #[inline(always)]
66 pub(crate) fn ptr_mut(&self) -> *mut Node<V> {
67 self.ptr
68 }
69
70 #[inline(always)]
73 pub fn get_ptr(&self, collection_state: MemoryState) -> Option<*mut Node<V>> {
74 self.state.eq(&collection_state).then_some(self.ptr)
75 }
76
77 #[inline(always)]
79 pub fn node_ptr(&self) -> NodePtr<V> {
80 NodePtr::new(self.ptr)
81 }
82
83 #[inline(always)]
90 pub fn is_valid_for<M, P>(&self, collection: &SelfRefCol<V, M, P>) -> bool
91 where
92 M: MemoryPolicy<V>,
93 P: PinnedVec<Node<V>>,
94 {
95 self.state == collection.memory_state()
96 && collection.nodes().contains_ptr(self.ptr)
97 && unsafe { &*self.ptr }.is_active()
98 }
99}