| 1 |
use std::collections::hash_map::RandomState; |
| 2 |
use std::hash::{BuildHasher, Hash}; |
| 3 |
|
| 4 |
#[cfg(not(feature = "abi_stable"))] |
| 5 |
mod basic_impl { |
| 6 |
pub type BoxImpl<T> = Box<T>; |
| 7 |
pub type HashMapImpl<K, V, S> = std::collections::HashMap<K, V, S>; |
| 8 |
pub type MutexImpl<T> = std::sync::Mutex<T>; |
| 9 |
pub type MutexGuardImpl<'a, T> = std::sync::MutexGuard<'a, T>; |
| 10 |
pub type IterImpl<'a, K, V> = std::collections::hash_map::Iter<'a, K, V>; |
| 11 |
pub type IntoIterImpl<K, V> = std::collections::hash_map::IntoIter<K, V>; |
| 12 |
|
| 13 |
pub fn box_into_inner_impl<T>(b: BoxImpl<T>) -> T { |
| 14 |
*b |
| 15 |
} |
| 16 |
|
| 17 |
pub fn mutex_lock_impl<'a, T>(m: &'a MutexImpl<T>) -> MutexGuardImpl<'a, T> { |
| 18 |
m.lock().unwrap() |
| 19 |
} |
| 20 |
|
| 21 |
pub fn mutex_into_inner_impl<T>(m: MutexImpl<T>) -> T { |
| 22 |
m.into_inner().unwrap() |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
#[cfg(not(feature = "abi_stable"))] |
| 27 |
use basic_impl::*; |
| 28 |
|
| 29 |
#[cfg(feature = "abi_stable")] |
| 30 |
mod abi_stable_impl { |
| 31 |
use abi_stable::{ |
| 32 |
external_types::RMutex, |
| 33 |
std_types::{RBox, RHashMap}, |
| 34 |
}; |
| 35 |
pub type BoxImpl<T> = RBox<T>; |
| 36 |
pub type HashMapImpl<K, V, S> = RHashMap<K, V, S>; |
| 37 |
pub type MutexImpl<T> = RMutex<T>; |
| 38 |
pub type MutexGuardImpl<'a, T> = |
| 39 |
abi_stable::external_types::parking_lot::mutex::RMutexGuard<'a, T>; |
| 40 |
pub type IterImpl<'a, K, V> = abi_stable::std_types::map::Iter<'a, K, V>; |
| 41 |
pub type IntoIterImpl<K, V> = abi_stable::std_types::map::IntoIter<K, V>; |
| 42 |
|
| 43 |
pub fn box_into_inner_impl<T>(b: BoxImpl<T>) -> T { |
| 44 |
RBox::into_inner(b) |
| 45 |
} |
| 46 |
|
| 47 |
pub fn mutex_lock_impl<'a, T>(m: &'a MutexImpl<T>) -> MutexGuardImpl<'a, T> { |
| 48 |
m.lock() |
| 49 |
} |
| 50 |
|
| 51 |
pub fn mutex_into_inner_impl<T>(m: MutexImpl<T>) -> T { |
| 52 |
m.into_inner() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[cfg(feature = "abi_stable")] |
| 57 |
use abi_stable_impl::*; |
| 58 |
|
| 59 |
/// An insert-only map for caching the result of functions |
| 60 |
#[cfg_attr(feature = "abi_stable", derive(abi_stable::StableAbi))] |
| 61 |
#[cfg_attr(feature = "abi_stable", repr(C))] |
| 62 |
pub struct CacheMap<K, V, S = RandomState> { |
| 63 |
inner: MutexImpl<HashMapImpl<K, BoxImpl<V>, S>>, |
| 64 |
} |
| 65 |
|
| 66 |
impl<K: Eq + Hash, V, S: BuildHasher + Default> Default for CacheMap<K, V, S> { |
| 67 |
fn default() -> Self { |
| 68 |
CacheMap { |
| 69 |
inner: MutexImpl::new(Default::default()), |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl<K: Eq + Hash, V, S: BuildHasher + Default> std::iter::FromIterator<(K, V)> |
| 75 |
for CacheMap<K, V, S> |
| 76 |
{ |
| 77 |
fn from_iter<T>(iter: T) -> Self |
| 78 |
where |
| 79 |
T: IntoIterator<Item = (K, V)>, |
| 80 |
{ |
| 81 |
CacheMap { |
| 82 |
inner: MutexImpl::new( |
| 83 |
iter.into_iter() |
| 84 |
.map(|(k, v)| (k, BoxImpl::new(v))) |
| 85 |
.collect(), |
| 86 |
), |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub struct IntoIter<K, V>(IntoIterImpl<K, BoxImpl<V>>); |
| 92 |
|
| 93 |
impl<K, V> Iterator for IntoIter<K, V> { |
| 94 |
type Item = (K, V); |
| 95 |
|
| 96 |
fn next(&mut self) -> Option<Self::Item> { |
| 97 |
self.0.next().map(|t| (t.0, box_into_inner_impl(t.1))) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl<K, V, S> IntoIterator for CacheMap<K, V, S> { |
| 102 |
type Item = (K, V); |
| 103 |
type IntoIter = IntoIter<K, V>; |
| 104 |
|
| 105 |
fn into_iter(self) -> Self::IntoIter { |
| 106 |
IntoIter(mutex_into_inner_impl(self.inner).into_iter()) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
pub struct Iter<'a, K, V, S> { |
| 111 |
iter: IterImpl<'a, K, BoxImpl<V>>, |
| 112 |
_guard: MutexGuardImpl<'a, HashMapImpl<K, BoxImpl<V>, S>>, |
| 113 |
} |
| 114 |
|
| 115 |
impl<'a, K, V, S> Iterator for Iter<'a, K, V, S> { |
| 116 |
type Item = (&'a K, &'a V); |
| 117 |
|
| 118 |
fn next(&mut self) -> Option<Self::Item> { |
| 119 |
self.iter.next().map(|t| (t.0, t.1.as_ref())) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl<'a, K, V, S> IntoIterator for &'a CacheMap<K, V, S> { |
| 124 |
type Item = (&'a K, &'a V); |
| 125 |
type IntoIter = Iter<'a, K, V, S>; |
| 126 |
|
| 127 |
fn into_iter(self) -> Self::IntoIter { |
| 128 |
let guard = mutex_lock_impl(&self.inner); |
| 129 |
let iter = unsafe { |
| 130 |
std::mem::transmute::<IterImpl<K, BoxImpl<V>>, IterImpl<K, BoxImpl<V>>>(guard.iter()) |
| 131 |
}; |
| 132 |
Iter { |
| 133 |
iter, |
| 134 |
_guard: guard, |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl<K: Eq + Hash, V, S: BuildHasher> CacheMap<K, V, S> { |
| 140 |
/// Fetch the value associated with the key, or run the provided function to insert one. |
| 141 |
/// |
| 142 |
/// # Example |
| 143 |
/// |
| 144 |
/// ``` |
| 145 |
/// use cachemap2::CacheMap; |
| 146 |
/// |
| 147 |
/// let m = CacheMap::new(); |
| 148 |
/// |
| 149 |
/// let fst = m.cache("key", || 5u32); |
| 150 |
/// let snd = m.cache("key", || 7u32); |
| 151 |
/// |
| 152 |
/// assert_eq!(*fst, *snd); |
| 153 |
/// assert_eq!(*fst, 5u32); |
| 154 |
/// ``` |
| 155 |
pub fn cache<F: FnOnce() -> V>(&self, key: K, f: F) -> &V { |
| 156 |
let v = std::ptr::NonNull::from( |
| 157 |
mutex_lock_impl(&self.inner) |
| 158 |
.entry(key) |
| 159 |
.or_insert_with(|| BoxImpl::new(f())) |
| 160 |
.as_ref(), |
| 161 |
); |
| 162 |
// Safety: We only support adding entries to the hashmap, and as long as a reference is |
| 163 |
// maintained the value will be present. |
| 164 |
unsafe { v.as_ref() } |
| 165 |
} |
| 166 |
|
| 167 |
/// Fetch the value associated with the key, or insert a default value. |
| 168 |
pub fn cache_default(&self, key: K) -> &V |
| 169 |
where |
| 170 |
V: Default, |
| 171 |
{ |
| 172 |
self.cache(key, || Default::default()) |
| 173 |
} |
| 174 |
|
| 175 |
/// Return whether the map contains the given key. |
| 176 |
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool |
| 177 |
where |
| 178 |
K: std::borrow::Borrow<Q>, |
| 179 |
Q: Hash + Eq, |
| 180 |
{ |
| 181 |
mutex_lock_impl(&self.inner).contains_key(key) |
| 182 |
} |
| 183 |
|
| 184 |
/// Return an iterator over the map. |
| 185 |
/// |
| 186 |
/// This iterator will lock the underlying map until it is dropped. |
| 187 |
pub fn iter(&self) -> Iter<K, V, S> { |
| 188 |
self.into_iter() |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl<K: Eq + Hash, V> CacheMap<K, V, RandomState> { |
| 193 |
/// Creates a new CacheMap |
| 194 |
pub fn new() -> Self { |
| 195 |
Default::default() |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
impl<K: Eq + Hash, V, S: BuildHasher + Default> CacheMap<K, V, S> { |
| 200 |
/// Creates a new CacheMap with the provided hasher |
| 201 |
pub fn with_hasher(hash_builder: S) -> Self { |
| 202 |
Self { |
| 203 |
inner: MutexImpl::new(HashMapImpl::with_hasher(hash_builder)), |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[cfg(test)] |
| 209 |
mod tests { |
| 210 |
use super::*; |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn single_insert() { |
| 214 |
let m = CacheMap::new(); |
| 215 |
|
| 216 |
let a = m.cache("key", || 21u32); |
| 217 |
assert_eq!(21, *a); |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn contains_key() { |
| 222 |
let m = CacheMap::new(); |
| 223 |
|
| 224 |
m.cache("key", || 21u32); |
| 225 |
assert!(m.contains_key("key")); |
| 226 |
assert!(!m.contains_key("other")); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn double_insert() { |
| 231 |
let m = CacheMap::new(); |
| 232 |
|
| 233 |
let a = m.cache("key", || 5u32); |
| 234 |
let b = m.cache("key", || 7u32); |
| 235 |
|
| 236 |
assert_eq!(*a, *b); |
| 237 |
assert_eq!(5, *a); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn insert_two() { |
| 242 |
let m = CacheMap::new(); |
| 243 |
|
| 244 |
let a = m.cache("a", || 5u32); |
| 245 |
let b = m.cache("b", || 7u32); |
| 246 |
|
| 247 |
assert_eq!(5, *a); |
| 248 |
assert_eq!(7, *b); |
| 249 |
|
| 250 |
let c = m.cache("a", || 9u32); |
| 251 |
let d = m.cache("b", || 11u32); |
| 252 |
|
| 253 |
assert_eq!(*a, *c); |
| 254 |
assert_eq!(*b, *d); |
| 255 |
|
| 256 |
assert_eq!(5, *a); |
| 257 |
assert_eq!(7, *b); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn iter() { |
| 262 |
use std::collections::HashMap; |
| 263 |
use std::iter::FromIterator; |
| 264 |
let m = CacheMap::new(); |
| 265 |
m.cache("a", || 5u32); |
| 266 |
m.cache("b", || 7u32); |
| 267 |
|
| 268 |
let mut expected = HashMap::<&'static str, u32>::from_iter([("a", 5u32), ("b", 7u32)]); |
| 269 |
|
| 270 |
for (k, v) in &m { |
| 271 |
assert!(expected.remove(k).expect("unexpected key") == *v); |
| 272 |
} |
| 273 |
|
| 274 |
assert!(expected.is_empty()); |
| 275 |
} |
| 276 |
} |