pub struct RefMut<'a, T: IdOrdItem>{ /* private fields */ }Expand description
A mutable reference to an IdOrdMap entry.
This is a wrapper around a &mut T that panics when dropped, if the
borrowed value’s key has changed since the wrapper was created.
§Change detection
It is illegal to change the keys of a borrowed &mut T. RefMut attempts
to enforce this invariant, and as part of that, it requires that the key
type implement Hash.
RefMut stores the Hash output of keys at creation time, and recomputes
these hashes when it is dropped or when Self::into_ref is called. If a
key changes, there’s a small but non-negligible chance that its hash value
stays the same1. In that case, the map will no longer
function correctly and might panic on access. This will not introduce memory
safety issues, however.
It is also possible to deliberately write pathological Hash
implementations that collide more often. (Don’t do this.)
Also, RefMut’s hash detection will not function if mem::forget is
called on it. If a key is changed and mem::forget is then called on the
RefMut, the IdOrdMap will no longer function correctly and might panic
on access. This will not introduce memory safety issues, however.
The issues here are similar to using interior mutability (e.g. RefCell or
Mutex) to mutate keys in a regular HashMap.
- The output of - Hashis a- u64, so the probability of an individual hash colliding by chance is 1/2⁶⁴. Due to the birthday problem, the probability of a collision by chance reaches 10⁻⁶ within around 6 × 10⁶ elements. ↩