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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
use super::ReadGuard; use crate::{inner::Inner, values::Values}; use std::borrow::Borrow; use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash}; use std::mem::ManuallyDrop; /// A live reference into the read half of an evmap. /// /// As long as this lives, the map being read cannot change, and if a writer attempts to /// call [`WriteHandle::refresh`], that call will block until this is dropped. /// /// Since the map remains immutable while this lives, the methods on this type all give you /// unguarded references to types contained in the map. #[derive(Debug)] pub struct MapReadRef<'rh, K, V, M = (), S = RandomState> where K: Hash + Eq, V: Eq + Hash, S: BuildHasher, { pub(super) guard: ReadGuard<'rh, Inner<K, ManuallyDrop<V>, M, S>>, } impl<'rh, K, V, M, S> MapReadRef<'rh, K, V, M, S> where K: Hash + Eq, V: Eq + Hash, S: BuildHasher, { /// Iterate over all key + valuesets in the map. /// /// Be careful with this function! While the iteration is ongoing, any writer that tries to /// refresh will block waiting on this reader to finish. pub fn iter(&self) -> ReadGuardIter<'_, K, V, S> { ReadGuardIter { iter: Some(self.guard.data.iter()), } } /// Returns the number of non-empty keys present in the map. pub fn len(&self) -> usize { self.guard.data.len() } /// Returns true if the map contains no elements. pub fn is_empty(&self) -> bool { self.guard.data.is_empty() } /// Get the current meta value. pub fn meta(&self) -> &M { &self.guard.meta } /// Returns a reference to the values corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed /// form *must* match those for the key type. /// /// Note that not all writes will be included with this read -- only those that have been /// refreshed by the writer. If no refresh has happened, or the map has been destroyed, this /// function returns `None`. pub fn get<'a, Q: ?Sized>(&'a self, key: &'_ Q) -> Option<&'a Values<V, S>> where K: Borrow<Q>, Q: Hash + Eq, { self.guard.data.get(key).map(Values::user_friendly) } /// Returns a guarded reference to _one_ value corresponding to the key. /// /// This is mostly intended for use when you are working with no more than one value per key. /// If there are multiple values stored for this key, there are no guarantees to which element /// is returned. /// /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed /// form *must* match those for the key type. /// /// Note that not all writes will be included with this read -- only those that have been /// refreshed by the writer. If no refresh has happened, or the map has been destroyed, this /// function returns `None`. pub fn get_one<'a, Q: ?Sized>(&'a self, key: &'_ Q) -> Option<&'a V> where K: Borrow<Q>, Q: Hash + Eq, { self.guard .data .get(key) .and_then(|values| values.user_friendly().get_one()) } /// Returns true if the map contains any values for the specified key. /// /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed /// form *must* match those for the key type. pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: Hash + Eq, { self.guard.data.contains_key(key) } /// Returns true if the map contains the specified value for the specified key. /// /// The key and value may be any borrowed form of the map's respective types, but `Hash` and /// `Eq` on the borrowed form *must* match. pub fn contains_value<Q: ?Sized, W: ?Sized>(&self, key: &Q, value: &W) -> bool where K: Borrow<Q>, V: Borrow<W>, Q: Hash + Eq, W: Hash + Eq, { self.guard .data .get(key) .map_or(false, |values| values.user_friendly().contains(value)) } } impl<'rh, K, Q, V, M, S> std::ops::Index<&'_ Q> for MapReadRef<'rh, K, V, M, S> where K: Eq + Hash + Borrow<Q>, V: Eq + Hash, Q: Eq + Hash + ?Sized, S: BuildHasher, { type Output = Values<V, S>; fn index(&self, key: &Q) -> &Self::Output { self.get(key).unwrap() } } impl<'rg, 'rh, K, V, M, S> IntoIterator for &'rg MapReadRef<'rh, K, V, M, S> where K: Eq + Hash, V: Eq + Hash, S: BuildHasher, { type Item = (&'rg K, &'rg Values<V, S>); type IntoIter = ReadGuardIter<'rg, K, V, S>; fn into_iter(self) -> Self::IntoIter { self.iter() } } /// An [`Iterator`] over keys and values in the evmap. #[derive(Debug)] pub struct ReadGuardIter<'rg, K, V, S> where K: Eq + Hash, V: Eq + Hash, S: BuildHasher, { iter: Option< <&'rg crate::inner::MapImpl<K, Values<ManuallyDrop<V>, S>, S> as IntoIterator>::IntoIter, >, } impl<'rg, K, V, S> Iterator for ReadGuardIter<'rg, K, V, S> where K: Eq + Hash, V: Eq + Hash, S: BuildHasher, { type Item = (&'rg K, &'rg Values<V, S>); fn next(&mut self) -> Option<Self::Item> { self.iter .as_mut() .and_then(|iter| iter.next().map(|(k, v)| (k, v.user_friendly()))) } }