key_node_list/
node.rs

1/// Trait for nodes that holds its previous and next key in
2/// [`KeyNodeList`](crate::KeyNodeList).
3pub trait Node {
4  /// Key type of the current `Node`.
5  type Key;
6
7  /// Returns a reference to the previous key of the current node,
8  /// or returns `None` if the current node is the first node in the list.
9  fn prev(&self) -> Option<&Self::Key>;
10
11  /// Returns a reference to the next key of the current node,
12  /// or returns `None` if the current node is the last node in the list.
13  fn next(&self) -> Option<&Self::Key>;
14
15  /// Returns a mutable reference to the previous key of the current node
16  /// so that [`KeyNodeList`](crate::KeyNodeList) can update the order of
17  /// the nodes.
18  fn prev_mut<T: NodeToken>(&mut self) -> &mut Option<Self::Key>;
19
20  /// Returns a mutable reference to the next key of the current node
21  /// so that [`KeyNodeList`](crate::KeyNodeList) can update the order of
22  /// the nodes.
23  fn next_mut<T: NodeToken>(&mut self) -> &mut Option<Self::Key>;
24}
25
26/// Implements [`Node`] trait for the specific structure.
27#[macro_export]
28macro_rules! impl_node {
29  (
30    $node:ident$(<$($g:tt),* $(,)?>)?
31    { Key = $key:ty, prev = $prev:ident, next = $next:ident $(,)? }
32  ) => {
33    impl$(<$($g),*>)? $crate::Node for $node$(<$($g),*>)? {
34      type Key = $key;
35
36      #[inline]
37      fn prev(&self) -> Option<&Self::Key> {
38        self.$prev.as_ref()
39      }
40
41      #[inline]
42      fn next(&self) -> Option<&Self::Key> {
43        self.$next.as_ref()
44      }
45
46      #[inline]
47      fn prev_mut<__: $crate::NodeToken>(&mut self) -> &mut Option<Self::Key> {
48        &mut self.$prev
49      }
50
51      #[inline]
52      fn next_mut<__: $crate::NodeToken>(&mut self) -> &mut Option<Self::Key> {
53        &mut self.$next
54      }
55    }
56  };
57}
58
59/// A generic node for the [`KeyNodeList`](crate::KeyNodeList).
60///
61/// `ValueNode` can hold any kind of value.
62#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub struct ValueNode<K, V> {
64  value: V,
65  prev: Option<K>,
66  next: Option<K>,
67}
68
69impl_node!(ValueNode<K, V> { Key = K, prev = prev, next = next});
70
71impl<K, V> ValueNode<K, V> {
72  /// Creates a new node with `value`.
73  pub fn new(value: V) -> Self {
74    Self {
75      value,
76      prev: None,
77      next: None,
78    }
79  }
80
81  /// Consumes this [`ValueNode`], returning the underlying value.
82  pub fn into_value(self) -> V {
83    self.value
84  }
85
86  /// Returns a reference to the node value.
87  pub fn value(&self) -> &V {
88    &self.value
89  }
90
91  /// Returns a mutable reference to the node value.
92  pub fn value_mut(&mut self) -> &mut V {
93    &mut self.value
94  }
95}
96
97impl<K, V> Default for ValueNode<K, V>
98where
99  V: Default,
100{
101  fn default() -> Self {
102    Self::new(V::default())
103  }
104}
105
106impl<K, V> From<V> for ValueNode<K, V> {
107  fn from(value: V) -> Self {
108    Self::new(value)
109  }
110}
111
112/// Token that used to update the keys in the `Node`.
113///
114/// Only the `key_node_list` crate holds the actual token.
115pub trait NodeToken: private::Sealed {}
116
117pub(crate) struct Token;
118impl NodeToken for Token {}
119impl private::Sealed for Token {}
120
121mod private {
122  pub trait Sealed {}
123}