Is there an elegant way to iterate over each key-value pair in this concurrent hash map?
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::RwLock;
const N_SHARDS: usize = 16;
#[derive(Default)]
struct ConcurrentHashMap<K, V>
where
K: Eq + Hash,
{
shards: [RwLock<HashMap<K, V>>; N_SHARDS],
}
Hopefully this suggests what I'm trying to accomplish:
impl<K, V> ConcurrentHashMap<K, V>
where
K: Eq + Hash,
{
fn iter_mut(&self) -> impl Iterator<Item = (&K, &mut V)> {
self.shards
.iter()
.flat_map(|shard| shard.write().expect("Poisoned").iter_mut())
}
}
error[E0515]: cannot return value referencing temporary value
--> src/lib.rs:22:31
|
22 | .flat_map(|shard| shard.write().expect("Poisoned").iter_mut())
| --------------------------------^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| temporary value created here