pub struct Ref<'a, K, V, S = RandomState> {
_guard: RwLockReadGuard<'a, HashMap<K, V, S>>,
k: &'a K,
v: &'a V,
}
pub struct SharedValue<T> {
value: UnsafeCell<T>,
}
unsafe fn _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>> {
debug_assert!(i < self.shards.len());
self.shards.get_unchecked(i).read()
}
fn _get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = unsafe { self._yield_read_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr = &*(kptr as *const K);
let vptr = &*(vptr as *const SharedValue<V>);
Some(Ref::new(shard, kptr, vptr.get()))
}
}
else {
None
}
}
The ownership of variable shard
belongs to the _get
function, but in the end the function returns a Ref
that causes shard
to move, besides the code doesn't panic with any error(e.g. cannot return value referencing local variable shard
), why is that?(above code is from dashmap)