1#[cfg(test)]
2mod tests;
3
4use {
5 crate::{
6 linear_storage::LinearStorage,
7 map::StableMap,
8 pos_vec::pos::{InUse, Pos},
9 },
10 core::{
11 fmt::{Debug, Formatter},
12 iter::FusedIterator,
13 },
14 hashbrown::hash_map,
15};
16
17pub struct Iter<'a, K, V> {
46 pub(crate) iter: hash_map::Iter<'a, K, Pos<InUse>>,
47 pub(crate) entries: &'a LinearStorage<V>,
48}
49
50impl<'a, K, V> Iterator for Iter<'a, K, V> {
51 type Item = (&'a K, &'a V);
52
53 fn next(&mut self) -> Option<Self::Item> {
54 let (k, pos) = self.iter.next()?;
55 let v = unsafe { self.entries.get_unchecked(pos) };
56 Some((k, v))
57 }
58
59 fn size_hint(&self) -> (usize, Option<usize>) {
60 self.iter.size_hint()
61 }
62}
63
64impl<'a, K, V, S> IntoIterator for &'a StableMap<K, V, S> {
65 type Item = (&'a K, &'a V);
66 type IntoIter = Iter<'a, K, V>;
67
68 fn into_iter(self) -> Self::IntoIter {
69 self.iter()
70 }
71}
72
73impl<K, V> Clone for Iter<'_, K, V> {
74 fn clone(&self) -> Self {
75 Self {
76 iter: self.iter.clone(),
77 entries: self.entries,
78 }
79 }
80}
81
82impl<K, V> Debug for Iter<'_, K, V>
83where
84 K: Debug,
85 V: Debug,
86{
87 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
88 f.debug_list().entries(self.clone()).finish()
89 }
90}
91
92impl<K, V> FusedIterator for Iter<'_, K, V> {}
93
94impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
95 fn len(&self) -> usize {
96 self.iter.len()
97 }
98}
99
100unsafe impl<K, V> Send for Iter<'_, K, V>
104where
105 K: Send,
106 V: Send,
107{
108}
109
110unsafe impl<K, V> Sync for Iter<'_, K, V>
114where
115 K: Sync,
116 V: Sync,
117{
118}