是否有一种优雅的方法可以迭代此并发哈希图中的每个键值对?
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],
}
希望这表明我正在尝试完成的事情:
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