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 unsafe fn raw_ptr(&self) -> *mut Node<Doubly<T>> {
10 unsafe { 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 ///
18 /// # SAFETY
19 ///
20 /// This method is unsafe as node pointers implement `Send` and `Sync`.
21 ///
22 /// It is safe dereference the received pointer if we know that `is_valid_for(col)` would
23 /// return `true` where `col` is the collection that this pointer is created from.
24 unsafe fn raw_ptr(&self) -> *mut Node<Doubly<T>>;
25
26 /// Returns a reference to the node.
27 ///
28 /// # Safety
29 ///
30 /// This method creates a reference directly from
31 /// the pointer without any checks.
32 ///
33 /// The caller is responsible for the validness
34 /// of the node pointer.
35 ///
36 /// Alternatively, you may use `NodeIdx` for safe access.
37 #[inline(always)]
38 unsafe fn node(&self) -> &Node<Doubly<T>> {
39 unsafe { &*self.raw_ptr() }
40 }
41
42 /// Returns a mutable reference to the node.
43 ///
44 /// # Safety
45 ///
46 /// This method creates a reference directly from
47 /// the pointer without any checks.
48 ///
49 /// The caller is responsible for the validness
50 /// of the node pointer.
51 ///
52 /// Alternatively, you may use `NodeIdx` for safe access.
53 #[inline(always)]
54 unsafe fn node_mut(&mut self) -> &mut Node<Doubly<T>> {
55 unsafe { &mut *self.raw_ptr() }
56 }
57
58 /// Returns the pointer to the next node if exists; None otherwise.
59 ///
60 /// # Safety
61 ///
62 /// This method creates a reference directly from
63 /// the pointer without any checks.
64 ///
65 /// The caller is responsible for the validness
66 /// of the node pointer.
67 ///
68 /// Alternatively, you may use `NodeIdx` for safe access.
69 #[inline(always)]
70 unsafe fn next(&self) -> Option<DoublyPtr<T>> {
71 unsafe { self.node() }.next().get()
72 }
73
74 /// Returns the pointer to the prev node if exists; None otherwise.
75 ///
76 /// # Safety
77 ///
78 /// This method creates a reference directly from
79 /// the pointer without any checks.
80 ///
81 /// The caller is responsible for the validness
82 /// of the node pointer.
83 ///
84 /// Alternatively, you may use `NodeIdx` for safe access.
85 #[inline(always)]
86 unsafe fn prev(&self) -> Option<DoublyPtr<T>> {
87 unsafe { self.node() }.prev().get()
88 }
89}