-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter.rs
More file actions
74 lines (61 loc) · 1.79 KB
/
Copy pathiter.rs
File metadata and controls
74 lines (61 loc) · 1.79 KB
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
use std::{
collections::{btree_map as map, BTreeMap},
fmt::Debug,
iter::FusedIterator,
pin::Pin,
sync::RwLockReadGuard,
};
use super::erase;
/// Iterator over key-value pairs of [super::PinnedMap].
pub struct Iter<'a, K, V> {
/// Shall not be read. Only kept here to prevent the map from being modified.
#[allow(unused)]
guard: RwLockReadGuard<'a, BTreeMap<K, Pin<Box<V>>>>,
inner: map::Iter<'a, K, Pin<Box<V>>>,
}
impl<'a, K, V> Iter<'a, K, V> {
pub(super) fn new(guard: RwLockReadGuard<'a, BTreeMap<K, Pin<Box<V>>>>) -> Self {
let inner = unsafe { std::mem::transmute(guard.values()) };
Self { guard, inner }
}
}
impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<(&'a K, &'a V)> {
self.inner.next().map(|(k, v)| (k, erase(v)))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
fn last(self) -> Option<(&'a K, &'a V)> {
self.inner.last().map(|(k, v)| (k, erase(v)))
}
fn min(mut self) -> Option<(&'a K, &'a V)>
where
(&'a K, &'a V): Ord,
{
self.next()
}
fn max(mut self) -> Option<(&'a K, &'a V)>
where
(&'a K, &'a V): Ord,
{
self.next_back()
}
}
impl<K, V> FusedIterator for Iter<'_, K, V> {}
impl<'a, K: 'a, V: 'a> DoubleEndedIterator for Iter<'a, K, V> {
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
self.inner.next_back().map(|(k, v)| (k, erase(v)))
}
}
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K: Debug, V: Debug> Debug for Iter<'_, K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.inner, f)
}
}