pinned_bucket/map/
values.rs

1use std::{
2    collections::{btree_map as map, BTreeMap},
3    fmt::Debug,
4    iter::FusedIterator,
5    pin::Pin,
6    sync::RwLockReadGuard,
7};
8
9use super::erase;
10
11/// Iterator over values of [super::PinnedMap].
12pub struct Values<'a, K, V> {
13    /// Shall not be read. Only kept here to prevent the map from being modified.
14    #[allow(unused)]
15    guard: RwLockReadGuard<'a, BTreeMap<K, Pin<Box<V>>>>,
16    inner: map::Values<'a, K, Pin<Box<V>>>,
17}
18
19impl<'a, K, V> Values<'a, K, V> {
20    pub(super) fn new(guard: RwLockReadGuard<'a, BTreeMap<K, Pin<Box<V>>>>) -> Self {
21        let inner = unsafe { std::mem::transmute(guard.values()) };
22        Self { guard, inner }
23    }
24}
25
26impl<'a, K, V> Iterator for Values<'a, K, V> {
27    type Item = &'a V;
28
29    fn next(&mut self) -> Option<&'a V> {
30        self.inner.next().map(erase)
31    }
32
33    fn size_hint(&self) -> (usize, Option<usize>) {
34        self.inner.size_hint()
35    }
36
37    fn last(mut self) -> Option<&'a V> {
38        self.next_back()
39    }
40}
41
42impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
43    fn next_back(&mut self) -> Option<&'a V> {
44        self.inner.next_back().map(erase)
45    }
46}
47
48impl<K, V> ExactSizeIterator for Values<'_, K, V> {
49    fn len(&self) -> usize {
50        self.inner.len()
51    }
52}
53
54impl<K, V> FusedIterator for Values<'_, K, V> {}
55
56impl<K, V: Debug> Debug for Values<'_, K, V> {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        Debug::fmt(&self.inner, f)
59    }
60}