1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[cfg(test)]
mod tests;
use {
crate::pos_vec::pos::{InUse, Pos},
core::{
fmt::{Debug, Formatter},
iter::FusedIterator,
},
hashbrown::hash_map,
};
/// An iterator over the keys of a `StableMap` in arbitrary order.
/// The iterator element type is `&'a K`.
///
/// This `struct` is created by the [`keys`] method on [`StableMap`]. See its
/// documentation for more.
///
/// [`keys`]: crate::StableMap::keys
/// [`StableMap`]: crate::StableMap
///
/// # Examples
///
/// ```
/// use stable_map::StableMap;
///
/// let map: StableMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
///
/// let mut keys = map.keys();
/// let mut vec = vec![keys.next(), keys.next(), keys.next()];
///
/// // The `Keys` iterator produces keys in arbitrary order, so the
/// // keys must be sorted to test them against a sorted array.
/// vec.sort_unstable();
/// assert_eq!(vec, [Some(&1), Some(&2), Some(&3)]);
///
/// // It is fused iterator
/// assert_eq!(keys.next(), None);
/// assert_eq!(keys.next(), None);
/// ```
pub struct Keys<'a, K> {
pub(crate) iter: hash_map::Keys<'a, K, Pos<InUse>>,
}
impl<'a, K> Iterator for Keys<'a, K> {
type Item = &'a K;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<K> Clone for Keys<'_, K> {
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}
impl<K> Debug for Keys<'_, K>
where
K: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<K> FusedIterator for Keys<'_, K> {}
impl<K> ExactSizeIterator for Keys<'_, K> {
fn len(&self) -> usize {
self.iter.len()
}
}
// SAFETY:
// - This impl is required because Pos<InUse>, Pos<Stored> allow for conflicting access
// but this API prevents this.
unsafe impl<K> Send for Keys<'_, K> where K: Send {}
// SAFETY:
// - This impl is required because Pos<InUse>, Pos<Stored> allow for conflicting access
// but this API prevents this.
unsafe impl<K> Sync for Keys<'_, K> where K: Sync {}