orx_selfref_col/
node.rs

1use crate::{Refs, Variant};
2use core::fmt::Debug;
3
4/// Node of the self referential collection.
5pub struct Node<V>
6where
7    V: Variant,
8{
9    data: Option<V::Item>,
10    prev: V::Prev,
11    next: V::Next,
12}
13
14impl<V> Node<V>
15where
16    V: Variant,
17{
18    /// Creates a new active node with the given `data`, and `prev` and `next` references.
19    pub fn new_active(data: V::Item, prev: V::Prev, next: V::Next) -> Self {
20        Self {
21            data: Some(data),
22            prev,
23            next,
24        }
25    }
26
27    /// Creates a new active node with the given `data` but with no connections.
28    pub fn new_free_node(data: V::Item) -> Self {
29        Self {
30            data: Some(data),
31            prev: Refs::empty(),
32            next: Refs::empty(),
33        }
34    }
35
36    // consuming
37
38    /// Takes and returns the data of the node, transitions the node into the closed state.
39    pub fn into_data(self) -> Option<V::Item> {
40        self.data
41    }
42
43    // ref
44
45    /// Returns a reference to the data of the node; None if the node is already closed.
46    pub fn data(&self) -> Option<&V::Item> {
47        self.data.as_ref()
48    }
49
50    /// Returns a reference to the previous references.
51    pub fn prev(&self) -> &V::Prev {
52        &self.prev
53    }
54
55    /// Returns a reference to the next references.
56    pub fn next(&self) -> &V::Next {
57        &self.next
58    }
59
60    /// Returns true if the node is active, false if it is closed.
61    #[inline(always)]
62    pub fn is_active(&self) -> bool {
63        self.data.is_some()
64    }
65
66    /// Returns true if the node is closed, false if it is active.
67    #[inline(always)]
68    pub fn is_closed(&self) -> bool {
69        self.data.is_none()
70    }
71
72    // mut
73
74    /// Returns a mutable reference to the underlying data.
75    pub fn data_mut(&mut self) -> Option<&mut V::Item> {
76        self.data.as_mut()
77    }
78
79    /// Returns a mutable reference to the previous references.
80    pub fn prev_mut(&mut self) -> &mut V::Prev {
81        &mut self.prev
82    }
83
84    /// Returns a mutable reference to the next references.
85    pub fn next_mut(&mut self) -> &mut V::Next {
86        &mut self.next
87    }
88
89    /// Closes the node and returns its data, and clears its connections.
90    ///
91    /// # Panics
92    ///
93    /// Panics if the node was already closed.
94    pub fn close(&mut self) -> V::Item {
95        self.prev.clear();
96        self.next.clear();
97        self.data.take().expect("must be an open node")
98    }
99
100    /// Swaps the data of the node with the `new_value` and returns the old value.
101    ///
102    /// # Panics
103    ///
104    /// Panics if the node was already closed.
105    pub fn swap_data(&mut self, new_value: V::Item) -> V::Item {
106        debug_assert!(self.is_active());
107        self.data.replace(new_value).expect("must be active")
108    }
109
110    /// Closes the node and returns its data.
111    ///
112    /// # Panics
113    ///
114    /// Panics if the node was already closed.
115    pub fn take_data(&mut self) -> Option<V::Item> {
116        self.data.take()
117    }
118}
119
120impl<V: Variant> Debug for Node<V>
121where
122    V::Item: Debug,
123{
124    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
125        f.debug_struct("Node")
126            .field("data", &self.data)
127            .field("prev", &self.prev)
128            .field("next", &self.next)
129            .finish()
130    }
131}