key_node_list/
iter.rs

1use crate::list::KeyNodeList;
2use crate::map::Map;
3use crate::node::Node;
4use std::hash::Hash;
5
6/// An owning iterator over the key-node paris of a [`KeyNodeList`].
7#[derive(Clone)]
8pub struct IntoIter<K, N, M> {
9  pub(crate) list: KeyNodeList<K, N, M>,
10}
11
12impl<K, N, M> Iterator for IntoIter<K, N, M>
13where
14  K: Hash + Eq + Clone,
15  N: Node<Key = K>,
16  M: Map<K, N>,
17{
18  type Item = (K, N);
19
20  #[inline]
21  fn next(&mut self) -> Option<Self::Item> {
22    self.list.pop_front()
23  }
24}
25
26/// An owning iterator over the keys of a [`KeyNodeList`].
27#[derive(Clone)]
28pub struct IntoKeys<K, N, M> {
29  pub(crate) iter: IntoIter<K, N, M>,
30}
31
32impl<K, N, M> Iterator for IntoKeys<K, N, M>
33where
34  K: Hash + Eq + Clone,
35  N: Node<Key = K>,
36  M: Map<K, N>,
37{
38  type Item = K;
39
40  #[inline]
41  fn next(&mut self) -> Option<Self::Item> {
42    self.iter.next().map(|(k, _)| k)
43  }
44}
45
46/// An owning iterator over the nodes of a [`KeyNodeList`].
47#[derive(Clone)]
48pub struct IntoNodes<K, N, M> {
49  pub(crate) iter: IntoIter<K, N, M>,
50}
51
52impl<K, N, M> Iterator for IntoNodes<K, N, M>
53where
54  K: Hash + Eq + Clone,
55  N: Node<Key = K>,
56  M: Map<K, N>,
57{
58  type Item = N;
59
60  #[inline]
61  fn next(&mut self) -> Option<Self::Item> {
62    self.iter.next().map(|(_, n)| n)
63  }
64}
65
66/// An iterator over the key-node pairs of a [`KeyNodeList`].
67#[derive(Clone)]
68pub struct Iter<'a, K, N, M> {
69  pub(crate) list: &'a KeyNodeList<K, N, M>,
70  pub(crate) key: Option<&'a K>,
71}
72
73impl<'a, K, N, M> Iterator for Iter<'a, K, N, M>
74where
75  K: Hash + Eq,
76  N: Node<Key = K>,
77  M: Map<K, N>,
78{
79  type Item = (&'a K, &'a N);
80
81  #[inline]
82  fn next(&mut self) -> Option<Self::Item> {
83    self.key.and_then(|k| {
84      self.list.node(k).map(|n| {
85        self.key = n.next();
86        (k, n)
87      })
88    })
89  }
90}
91
92/// An iterator over the keys of a [`KeyNodeList`].
93#[derive(Clone)]
94pub struct Keys<'a, K, N, M> {
95  pub(crate) iter: Iter<'a, K, N, M>,
96}
97
98impl<'a, K, N, M> Iterator for Keys<'a, K, N, M>
99where
100  K: Hash + Eq,
101  N: Node<Key = K>,
102  M: Map<K, N>,
103{
104  type Item = &'a K;
105
106  #[inline]
107  fn next(&mut self) -> Option<Self::Item> {
108    self.iter.next().map(|(k, _)| k)
109  }
110}
111
112/// An iterator over the nodes of a [`KeyNodeList`].
113#[derive(Clone)]
114pub struct Nodes<'a, K, N, M> {
115  pub(crate) iter: Iter<'a, K, N, M>,
116}
117
118impl<'a, K, N, M> Iterator for Nodes<'a, K, N, M>
119where
120  K: Hash + Eq,
121  N: Node<Key = K>,
122  M: Map<K, N>,
123{
124  type Item = &'a N;
125
126  #[inline]
127  fn next(&mut self) -> Option<Self::Item> {
128    self.iter.next().map(|(_, n)| n)
129  }
130}