Skip to main content

DerefMut

Trait DerefMut 

1.0.0 (const: unstable) · Source
pub trait DerefMut: Deref {
    // Required method
    fn deref_mut(&mut self) -> &mut Self::Target;
}
Expand description

Used for mutable dereferencing operations, like in *v = 1;.

In addition to being used for explicit dereferencing operations with the (unary) * operator in mutable contexts, DerefMut is also used implicitly by the compiler in many circumstances. This mechanism is called “mutable deref coercion”. In immutable contexts, Deref is used.

Warning: Deref coercion is a powerful language feature which has far-reaching implications for every type that implements DerefMut. The compiler will silently insert calls to DerefMut::deref_mut. For this reason, one should be careful about implementing DerefMut and only do so when mutable deref coercion is desirable. See the Deref docs for advice on when this is typically desirable or undesirable.

Types that implement DerefMut or Deref are often called “smart pointers” and the mechanism of deref coercion has been specifically designed to facilitate the pointer-like behavior that name suggests. Often, the purpose of a “smart pointer” type is to change the ownership semantics of a contained value (for example, Rc or Cow) or the storage semantics of a contained value (for example, Box).

§Mutable deref coercion

If T implements DerefMut<Target = U>, and v is a value of type T, then:

  • In mutable contexts, *v (where T is neither a reference nor a raw pointer) is equivalent to *DerefMut::deref_mut(&mut v).
  • Values of type &mut T are coerced to values of type &mut U
  • T implicitly implements all the (mutable) methods of the type U.

For more details, visit the chapter in The Rust Programming Language as well as the reference sections on the dereference operator, method resolution and type coercions.

§Fallibility

This trait’s method should never unexpectedly fail. Deref coercion means the compiler will often insert calls to DerefMut::deref_mut implicitly. Failure during dereferencing can be extremely confusing when DerefMut is invoked implicitly. In the majority of uses it should be infallible, though it may be acceptable to panic if the type is misused through programmer error, for example.

However, infallibility is not enforced and therefore not guaranteed. As such, unsafe code should not rely on infallibility in general for soundness.

§Examples

A struct with a single field which is modifiable by dereferencing the struct.

use std::ops::{Deref, DerefMut};

struct DerefMutExample<T> {
    value: T
}

impl<T> Deref for DerefMutExample<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T> DerefMut for DerefMutExample<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

let mut x = DerefMutExample { value: 'a' };
*x = 'b';
assert_eq!('b', x.value);

Required Methods§

1.0.0 (const: unstable) · Source

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

Mutably dereferences the value.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

1.0.0 · Source§

impl<T> !DerefMut for &T
where T: ?Sized,

Source§

impl DerefMut for Error

Available on crate feature std or non-anyhow_no_core_error only.
Source§

impl DerefMut for IndexedZip

Source§

impl DerefMut for ByteString

1.3.0 · Source§

impl DerefMut for String

1.44.0 · Source§

impl DerefMut for OsString

Source§

impl DerefMut for BStr

Source§

impl DerefMut for BString

Source§

impl DerefMut for BoxBytes

Source§

impl DerefMut for BytesMut

Source§

impl DerefMut for IoVec

Source§

impl DerefMut for UnixReady

Source§

impl DerefMut for Asn1BitString

Source§

impl DerefMut for Asn1Enumerated

Source§

impl DerefMut for Asn1GeneralizedTime

Source§

impl DerefMut for Asn1Integer

Source§

impl DerefMut for Asn1Object

Source§

impl DerefMut for Asn1OctetString

Source§

impl DerefMut for Asn1String

Source§

impl DerefMut for Asn1Time

Source§

impl DerefMut for BigNum

Source§

impl DerefMut for BigNumContext

Source§

impl DerefMut for Cipher

Source§

impl DerefMut for CipherCtx

Source§

impl DerefMut for CmsContentInfo

Source§

impl DerefMut for Conf

Source§

impl DerefMut for DsaSig

Source§

impl DerefMut for EcGroup

Source§

impl DerefMut for EcPoint

Source§

impl DerefMut for EcdsaSig

Source§

impl DerefMut for DigestBytes

Source§

impl DerefMut for LibCtx

Source§

impl DerefMut for Md

Source§

impl DerefMut for MdCtx

Source§

impl DerefMut for OcspBasicResponse

Source§

impl DerefMut for OcspCertId

Source§

impl DerefMut for OcspOneReq

Source§

impl DerefMut for OcspRequest

Source§

impl DerefMut for OcspResponse

Source§

impl DerefMut for Pkcs7

Source§

impl DerefMut for Pkcs7Signed

Source§

impl DerefMut for Pkcs7SignerInfo

Source§

impl DerefMut for Pkcs12

Source§

impl DerefMut for Provider

Source§

impl DerefMut for SrtpProtectionProfile

Source§

impl DerefMut for ConnectConfiguration

Source§

impl DerefMut for SslAcceptorBuilder

Source§

impl DerefMut for SslConnectorBuilder

Source§

impl DerefMut for Ssl

Source§

impl DerefMut for SslCipher

Source§

impl DerefMut for SslContext

Source§

impl DerefMut for SslSession

Source§

impl DerefMut for OpensslString

Source§

impl DerefMut for X509Store

Source§

impl DerefMut for X509StoreBuilder

Source§

impl DerefMut for AccessDescription

Source§

impl DerefMut for DistPoint

Source§

impl DerefMut for DistPointName

Source§

impl DerefMut for GeneralName

Source§

impl DerefMut for X509

Source§

impl DerefMut for X509Algorithm

Source§

impl DerefMut for X509Crl

Source§

impl DerefMut for X509Extension

Source§

impl DerefMut for X509Name

Source§

impl DerefMut for X509NameEntry

Source§

impl DerefMut for X509Object

Source§

impl DerefMut for X509Req

Source§

impl DerefMut for X509Revoked

Source§

impl DerefMut for X509StoreContext

Source§

impl DerefMut for X509VerifyParam

Source§

impl DerefMut for OtterOutput

Source§

impl DerefMut for ByteStr

Source§

impl DerefMut for InstanceGuard<'_>

Source§

impl DerefMut for LinksTable

Source§

impl DerefMut for MgmtChannelForGame

1.68.0 · Source§

impl DerefMut for PathBuf

Source§

impl<'a> DerefMut for socket2::MaybeUninitSlice<'a>

Source§

impl<'a> DerefMut for socket2::MaybeUninitSlice<'a>

1.36.0 · Source§

impl<'a> DerefMut for IoSliceMut<'a>

Source§

impl<'a, R, T> DerefMut for otter_nodejs_tests::parking_lot::lock_api::MappedMutexGuard<'a, R, T>
where R: RawMutex + 'a, T: 'a + ?Sized,

Source§

impl<'a, R, T> DerefMut for otter_nodejs_tests::parking_lot::lock_api::MappedRwLockWriteGuard<'a, R, T>
where R: RawRwLock + 'a, T: 'a + ?Sized,

Source§

impl<'a, R, T> DerefMut for otter_nodejs_tests::parking_lot::lock_api::MutexGuard<'a, R, T>
where R: RawMutex + 'a, T: 'a + ?Sized,

Source§

impl<'a, R, T> DerefMut for otter_nodejs_tests::parking_lot::lock_api::RwLockWriteGuard<'a, R, T>
where R: RawRwLock + 'a, T: 'a + ?Sized,

Source§

impl<'a, T> DerefMut for tokio::sync::mutex::MappedMutexGuard<'a, T>
where T: ?Sized,

Source§

impl<'a, T> DerefMut for Locked<'a, T>

Source§

impl<'a, T, A> DerefMut for alloc::vec::peek_mut::PeekMut<'a, T, A>
where A: Allocator,

Source§

impl<'a, T, F> DerefMut for PoolGuard<'a, T, F>
where T: Send, F: Fn() -> T,

Source§

impl<'g, T> DerefMut for otter_nodejs_tests::otter_support::debugmutex::MutexGuard<'g, T>
where T: DebugIdentify,

Source§

impl<A> DerefMut for SmallVec<A>
where A: Array,

Source§

impl<B, T> DerefMut for Ref<B, T>

Source§

impl<I> DerefMut for SubImage<I>
where I: DerefMut,

Source§

impl<I, A> DerefMut for IndexVec<I, A>
where I: Idx,

Source§

impl<L, R> DerefMut for Either<L, R>
where L: DerefMut, R: DerefMut<Target = <L as Deref>::Target>,

Source§

impl<P, Container> DerefMut for ImageBuffer<P, Container>
where P: Pixel, Container: Deref<Target = [<P as Pixel>::Subpixel]> + DerefMut,

1.33.0 (const: unstable) · Source§

impl<Ptr> DerefMut for Pin<Ptr>
where Ptr: Deref, PinHelper<Ptr>: PinDerefMutHelper<Target = <Pin<Ptr> as Deref>::Target>,

Source§

impl<S> DerefMut for Ascii<S>

Source§

impl<S> DerefMut for UniCase<S>

1.0.0 (const: unstable) · Source§

impl<T> DerefMut for &mut T
where T: ?Sized,

Source§

impl<T> DerefMut for ThinBox<T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::nonpoison::mutex::MappedMutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::nonpoison::mutex::MutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::nonpoison::rwlock::MappedRwLockWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::nonpoison::rwlock::RwLockWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::poison::mutex::MappedMutexGuard<'_, T>
where T: ?Sized,

1.0.0 · Source§

impl<T> DerefMut for std::sync::poison::mutex::MutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for std::sync::poison::rwlock::MappedRwLockWriteGuard<'_, T>
where T: ?Sized,

1.0.0 · Source§

impl<T> DerefMut for std::sync::poison::rwlock::RwLockWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for Owned<T>
where T: Pointable + ?Sized,

Source§

impl<T> DerefMut for CachePadded<T>

Source§

impl<T> DerefMut for ShardedLockWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for futures_util::lock::mutex::MutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for futures_util::lock::mutex::OwnedMutexGuard<T>
where T: ?Sized,

Source§

impl<T> DerefMut for Dh<T>

Source§

impl<T> DerefMut for Dsa<T>

Source§

impl<T> DerefMut for EcKey<T>

Source§

impl<T> DerefMut for PKey<T>

Source§

impl<T> DerefMut for PkeyCtx<T>

Source§

impl<T> DerefMut for Rsa<T>

Source§

impl<T> DerefMut for Stack<T>
where T: Stackable,

Source§

impl<T> DerefMut for X509Lookup<T>

Source§

impl<T> DerefMut for X509LookupMethod<T>

Source§

impl<T> DerefMut for tokio::sync::mutex::MutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for tokio::sync::mutex::OwnedMutexGuard<T>
where T: ?Sized,

Source§

impl<T> DerefMut for OwnedRwLockWriteGuard<T>
where T: ?Sized,

Source§

impl<T> DerefMut for tokio::sync::rwlock::write_guard::RwLockWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for RwLockMappedWriteGuard<'_, T>
where T: ?Sized,

Source§

impl<T> DerefMut for ReadOnly<T>
where T: Immutable + ?Sized,

Source§

impl<T> DerefMut for Unalign<T>
where T: Unaligned,

Source§

impl<T> DerefMut for Serde<T>

1.0.0 (const: unstable) · Source§

impl<T> DerefMut for RefMut<'_, T>
where T: ?Sized,

1.9.0 (const: unstable) · Source§

impl<T> DerefMut for AssertUnwindSafe<T>

1.20.0 (const: unstable) · Source§

impl<T> DerefMut for ManuallyDrop<T>
where T: ?Sized,

Source§

impl<T> DerefMut for OrderedFloat<T>
where T: Float,

1.0.0 · Source§

impl<T, A> DerefMut for Box<T, A>
where A: Allocator, T: ?Sized,

1.12.0 · Source§

impl<T, A> DerefMut for alloc::collections::binary_heap::PeekMut<'_, T, A>
where T: Ord, A: Allocator,

Source§

impl<T, A> DerefMut for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

Source§

impl<T, A> DerefMut for UniqueArc<T, A>
where A: Allocator, T: ?Sized,

1.0.0 (const: unstable) · Source§

impl<T, A> DerefMut for Vec<T, A>
where A: Allocator,

1.89.0 · Source§

impl<T, F> DerefMut for LazyLock<T, F>
where F: FnOnce() -> T,

1.89.0 · Source§

impl<T, F> DerefMut for LazyCell<T, F>
where F: FnOnce() -> T,

Source§

impl<T, F> DerefMut for otter_nodejs_tests::lazy_regex::Lazy<T, F>
where F: FnOnce() -> T,

Source§

impl<T, F> DerefMut for otter_nodejs_tests::once_cell::unsync::Lazy<T, F>
where F: FnOnce() -> T,

Source§

impl<T, F> DerefMut for DropGuard<T, F>
where F: FnOnce(T),

Source§

impl<T, F, S> DerefMut for ScopeGuard<T, F, S>
where F: FnOnce(T), S: Strategy,

Source§

impl<T, N> DerefMut for GenericArray<T, N>
where N: ArrayLength<T>,

Source§

impl<T, U> DerefMut for futures_util::lock::mutex::MappedMutexGuard<'_, T, U>
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> DerefMut for OwnedMappedMutexGuard<T, U>
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> DerefMut for OwnedRwLockMappedWriteGuard<T, U>
where T: ?Sized, U: ?Sized,

Source§

impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP>

Source§

impl<const CAP: usize> DerefMut for ArrayString<CAP>