orx_linked_list/pointers/singly_ptr.rs
1use crate::Singly;
2use orx_selfref_col::{Node, NodePtr};
3
4/// A node pointer in a Singly linked list.
5pub type SinglyPtr<T> = NodePtr<Singly<T>>;
6
7impl<T> SinglyPointer<T> for SinglyPtr<T> {
8 #[inline(always)]
9 fn raw_ptr(&self) -> *mut Node<Singly<T>> {
10 self.ptr() as *mut Node<Singly<T>>
11 }
12}
13
14/// A node pointer in a Singly linked list.
15pub trait SinglyPointer<T> {
16 /// Returns the raw pointer to the node.
17 fn raw_ptr(&self) -> *mut Node<Singly<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<Singly<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<Singly<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<SinglyPtr<T>> {
64 unsafe { self.node() }.next().get().cloned()
65 }
66}