1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use super::NodePtr;
use crate::{MemoryState, Node, Variant};
use core::fmt::Debug;

/// A node index providing safe and constant time access to elements
/// of the self referential collection.
#[derive(Copy)]
pub struct NodeIdx<V: Variant> {
    ptr: *mut Node<V>,
    state: MemoryState,
}

impl<V: Variant> Clone for NodeIdx<V> {
    fn clone(&self) -> Self {
        Self {
            ptr: self.ptr,
            state: self.state,
        }
    }
}

impl<V: Variant> Debug for NodeIdx<V> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("NodeIdx")
            .field("ptr", &self.ptr)
            .field("state", &self.state)
            .finish()
    }
}

impl<V: Variant> PartialEq for NodeIdx<V> {
    fn eq(&self, other: &Self) -> bool {
        self.ptr == other.ptr && self.state == other.state
    }
}

impl<V: Variant> Eq for NodeIdx<V> {}

impl<V> NodeIdx<V>
where
    V: Variant,
{
    /// Creates a new index for the element at the given `node_ptr`
    /// and the collection with the given `state`.
    #[inline(always)]
    pub fn new(state: MemoryState, node_ptr: &NodePtr<V>) -> Self {
        Self {
            ptr: node_ptr.ptr(),
            state,
        }
    }

    /// Checks whether or not the `state` of the index matches that of this index.
    #[inline(always)]
    pub fn is_in_state(&self, state: MemoryState) -> bool {
        self.state == state
    }

    #[inline(always)]
    pub(crate) fn ptr(&self) -> *const Node<V> {
        self.ptr
    }

    #[inline(always)]
    pub(crate) fn ptr_mut(&self) -> *mut Node<V> {
        self.ptr
    }

    /// Returns the node pointer if the index is in the same state as the `collection_state`,
    /// None otherwise.
    #[inline(always)]
    pub fn get_ptr(&self, collection_state: MemoryState) -> Option<*mut Node<V>> {
        self.state.eq(&collection_state).then_some(self.ptr)
    }

    /// Converts the node index into a node pointer.
    #[inline(always)]
    pub fn node_ptr(&self) -> NodePtr<V> {
        NodePtr::new(self.ptr)
    }
}