1use crate::{Refs, Variant};
2use core::fmt::Debug;
3
4pub 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 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 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 pub fn into_data(self) -> Option<V::Item> {
40 self.data
41 }
42
43 pub fn data(&self) -> Option<&V::Item> {
47 self.data.as_ref()
48 }
49
50 pub fn prev(&self) -> &V::Prev {
52 &self.prev
53 }
54
55 pub fn next(&self) -> &V::Next {
57 &self.next
58 }
59
60 #[inline(always)]
62 pub fn is_active(&self) -> bool {
63 self.data.is_some()
64 }
65
66 #[inline(always)]
68 pub fn is_closed(&self) -> bool {
69 self.data.is_none()
70 }
71
72 pub fn data_mut(&mut self) -> Option<&mut V::Item> {
76 self.data.as_mut()
77 }
78
79 pub fn prev_mut(&mut self) -> &mut V::Prev {
81 &mut self.prev
82 }
83
84 pub fn next_mut(&mut self) -> &mut V::Next {
86 &mut self.next
87 }
88
89 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 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 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}