orx_linked_list/pointers/doubly_ptr.rs
1use crate::Doubly;
2use orx_selfref_col::{Node, NodePtr};
3
4/// A node pointer in a doubly linked list.
5pub type DoublyPtr<T> = NodePtr<Doubly<T>>;
6
7impl<T> DoublyPointer<T> for DoublyPtr<T> {
8 #[inline(always)]
9 fn raw_ptr(&self) -> *mut Node<Doubly<T>> {
10 self.ptr() as *mut Node<Doubly<T>>
11 }
12}
13
14/// A node pointer in a doubly linked list.
15pub trait DoublyPointer<T> {
16 /// Returns the raw pointer to the node.
17 fn raw_ptr(&self) -> *mut Node<Doubly<T>>;
18
19 /// Returns a reference to the node.
20 ///
21 /// # Safety
22 ///
23 /// This method creates a reference directly from
24 /// the pointer without any checks.
25 ///
26 /// The caller is responsible for the validness
27 /// of the node pointer.
28 ///
29 /// Alternatively, you may use `NodeIdx` for safe access.
30 #[inline(always)]
31 unsafe fn node(&self) -> &Node<Doubly<T>> {
32 unsafe { &*self.raw_ptr() }
33 }
34
35 /// Returns a mutable reference to the node.
36 ///
37 /// # Safety
38 ///
39 /// This method creates a reference directly from
40 /// the pointer without any checks.
41 ///
42 /// The caller is responsible for the validness
43 /// of the node pointer.
44 ///
45 /// Alternatively, you may use `NodeIdx` for safe access.
46 #[inline(always)]
47 unsafe fn node_mut(&mut self) -> &mut Node<Doubly<T>> {
48 unsafe { &mut *self.raw_ptr() }
49 }
50
51 /// Returns the pointer to the next node if exists; None otherwise.
52 ///
53 /// # Safety
54 ///
55 /// This method creates a reference directly from
56 /// the pointer without any checks.
57 ///
58 /// The caller is responsible for the validness
59 /// of the node pointer.
60 ///
61 /// Alternatively, you may use `NodeIdx` for safe access.
62 #[inline(always)]
63 unsafe fn next(&self) -> Option<DoublyPtr<T>> {
64 unsafe { self.node() }.next().get().cloned()
65 }
66
67 /// Returns the pointer to the prev node if exists; None otherwise.
68 ///
69 /// # Safety
70 ///
71 /// This method creates a reference directly from
72 /// the pointer without any checks.
73 ///
74 /// The caller is responsible for the validness
75 /// of the node pointer.
76 ///
77 /// Alternatively, you may use `NodeIdx` for safe access.
78 #[inline(always)]
79 unsafe fn prev(&self) -> Option<DoublyPtr<T>> {
80 unsafe { self.node() }.prev().get().cloned()
81 }
82}