Struct RefMut

Source
pub struct RefMut<'a, T: IdHashItem, S: Clone + BuildHasher = DefaultHashBuilder> { /* private fields */ }
Expand description

A mutable reference to an IdHashMap item.

This is a wrapper around a &mut T that panics when dropped, if the borrowed value’s keys have changed since the wrapper was created.

§Change detection

It is illegal to change the key of a borrowed &mut T. RefMut attempts to enforce this invariant.

RefMut stores the Hash output of the key at creation time, and recomputes this hash 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, as long as the new key is not the same as another existing one, internal invariants are not violated and the IdHashMap will continue to work correctly. (But don’t rely on this!)

It is also possible to deliberately write pathological Hash implementations that collide more often. (Don’t do this either.)

Also, RefMut’s hash detection will not function if mem::forget is called on it. If the key is changed and mem::forget is then called on the RefMut, the IdHashMap will stop functioning correctly. 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.


  1. The output of Hash is 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. 

Implementations§

Source§

impl<'a, T: IdHashItem, S: Clone + BuildHasher> RefMut<'a, T, S>

Source

pub fn reborrow(&mut self) -> RefMut<'_, T, S>

Borrows self into a shorter-lived RefMut.

This RefMut will also check hash equality on drop.

Source

pub fn into_ref(self) -> &'a T

Converts this RefMut into a &'a T.

Trait Implementations§

Source§

impl<T: IdHashItem + Debug, S: Clone + BuildHasher> Debug for RefMut<'_, T, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: IdHashItem, S: Clone + BuildHasher> Deref for RefMut<'_, T, S>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: IdHashItem, S: Clone + BuildHasher> DerefMut for RefMut<'_, T, S>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: IdHashItem, S: Clone + BuildHasher> Drop for RefMut<'_, T, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T, S> Freeze for RefMut<'a, T, S>
where S: Freeze,

§

impl<'a, T, S> RefUnwindSafe for RefMut<'a, T, S>

§

impl<'a, T, S> Send for RefMut<'a, T, S>
where S: Send, T: Send,

§

impl<'a, T, S> Sync for RefMut<'a, T, S>
where S: Sync, T: Sync,

§

impl<'a, T, S> Unpin for RefMut<'a, T, S>
where S: Unpin,

§

impl<'a, T, S = RandomState> !UnwindSafe for RefMut<'a, T, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.