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
Hash
is au64
, 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. ↩
Implementations§
Source§impl<'a, T: IdOrdItem> RefMut<'a, T>
impl<'a, T: IdOrdItem> RefMut<'a, T>
Sourcepub fn reborrow<'b>(&'b mut self) -> RefMut<'b, T>
pub fn reborrow<'b>(&'b mut self) -> RefMut<'b, T>
Borrows self into a shorter-lived RefMut
.
This RefMut
will also check hash equality on drop.
Note: currently, due to limitations in the Rust borrow checker, this
effectively requires that T: 'static
. Relaxing this requirement should
be possible in principle.