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
82
use crate::Doubly;
use orx_selfref_col::{Node, NodePtr};

/// A node pointer in a doubly linked list.
pub type DoublyPtr<T> = NodePtr<Doubly<T>>;

impl<T> DoublyPointer<T> for DoublyPtr<T> {
    #[inline(always)]
    fn raw_ptr(&self) -> *mut Node<Doubly<T>> {
        self.ptr()
    }
}

/// A node pointer in a doubly linked list.
pub trait DoublyPointer<T> {
    /// Returns the raw pointer to the node.
    fn raw_ptr(&self) -> *mut Node<Doubly<T>>;

    /// Returns a reference to the node.
    ///
    /// # Safety
    ///
    /// This method creates a reference directly from
    /// the pointer without any checks.
    ///
    /// The caller is responsible for the validness
    /// of the node pointer.
    ///
    /// Alternatively, you may use `NodeIdx` for safe access.
    #[inline(always)]
    unsafe fn node(&self) -> &Node<Doubly<T>> {
        &*self.raw_ptr()
    }

    /// Returns a mutable reference to the node.
    ///
    /// # Safety
    ///
    /// This method creates a reference directly from
    /// the pointer without any checks.
    ///
    /// The caller is responsible for the validness
    /// of the node pointer.
    ///
    /// Alternatively, you may use `NodeIdx` for safe access.
    #[inline(always)]
    unsafe fn node_mut(&mut self) -> &mut Node<Doubly<T>> {
        &mut *self.raw_ptr()
    }

    /// Returns the pointer to the next node if exists; None otherwise.
    ///
    /// # Safety
    ///
    /// This method creates a reference directly from
    /// the pointer without any checks.
    ///
    /// The caller is responsible for the validness
    /// of the node pointer.
    ///
    /// Alternatively, you may use `NodeIdx` for safe access.
    #[inline(always)]
    unsafe fn next(&self) -> Option<DoublyPtr<T>> {
        self.node().next().get()
    }

    /// Returns the pointer to the prev node if exists; None otherwise.
    ///
    /// # Safety
    ///
    /// This method creates a reference directly from
    /// the pointer without any checks.
    ///
    /// The caller is responsible for the validness
    /// of the node pointer.
    ///
    /// Alternatively, you may use `NodeIdx` for safe access.
    #[inline(always)]
    unsafe fn prev(&self) -> Option<DoublyPtr<T>> {
        self.node().prev().get()
    }
}