pub trait Deref {
    type Target: ?Sized;

    // Required method
    fn deref(&self) -> &Self::Target;
}
Expand description

Used for immutable dereferencing operations, like *v.

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

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

Types that implement Deref or DerefMut are often called “smart pointers” and the mechanism of deref coercion has been specifically designed to facilitate the pointer-like behaviour 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).

Deref coercion

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

  • In immutable contexts, *v (where T is neither a reference nor a raw pointer) is equivalent to *Deref::deref(&v).
  • Values of type &T are coerced to values of type &U
  • T implicitly implements all the methods of the type U which take the &self receiver.

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.

When to implement Deref or DerefMut

The same advice applies to both deref traits. In general, deref traits should be implemented if:

  1. a value of the type transparently behaves like a value of the target type;
  2. the implementation of the deref function is cheap; and
  3. users of the type will not be surprised by any deref coercion behaviour.

In general, deref traits should not be implemented if:

  1. the deref implementations could fail unexpectedly; or
  2. the type has methods that are likely to collide with methods on the target type; or
  3. committing to deref coercion as part of the public API is not desirable.

Note that there’s a large difference between implementing deref traits generically over many target types, and doing so only for specific target types.

Generic implementations, such as for Box<T> (which is generic over every type and dereferences to T) should be careful to provide few or no methods, since the target type is unknown and therefore every method could collide with one on the target type, causing confusion for users. impl<T> Box<T> has no methods (though several associated functions), partly for this reason.

Specific implementations, such as for String (whose Deref implementation has Target = str) can have many methods, since avoiding collision is much easier. String and str both have many methods, and String additionally behaves as if it has every method of str because of deref coercion. The implementing type may also be generic while the implementation is still specific in this sense; for example, Vec<T> dereferences to [T], so methods of T are not applicable.

Consider also that deref coericion means that deref traits are a much larger part of a type’s public API than any other trait as it is implicitly called by the compiler. Therefore, it is advisable to consider whether this is something you are comfortable supporting as a public API.

The AsRef and Borrow traits have very similar signatures to Deref. It may be desirable to implement either or both of these, whether in addition to or rather than deref traits. See their documentation for details.

Fallibility

This trait’s method should never unexpectedly fail. Deref coercion means the compiler will often insert calls to Deref::deref implicitly. Failure during dereferencing can be extremely confusing when Deref 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 accessible by dereferencing the struct.

use std::ops::Deref;

struct DerefExample<T> {
    value: T
}

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

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

let x = DerefExample { value: 'a' };
assert_eq!('a', *x);

Required Associated Types§

source

type Target: ?Sized

The resulting type after dereferencing.

Required Methods§

source

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

Dereferences the value.

Implementors§

source§

impl Deref for CString

§

type Target = CStr

source§

impl Deref for String

§

type Target = str

source§

impl Deref for OsString

source§

impl Deref for PathBuf

§

type Target = Path

source§

impl Deref for BHP_256

§

type Target = BHP<Testnet3, 3, 57>

source§

impl Deref for BHP_512

§

type Target = BHP<Testnet3, 6, 43>

source§

impl Deref for BHP_768

§

type Target = BHP<Testnet3, 15, 23>

source§

impl Deref for BHP_1024

§

type Target = BHP<Testnet3, 8, 54>

source§

impl Deref for CREDITS_PROVING_KEYS

§

type Target = IndexMap<String, Arc<CircuitProvingKey<<Console as Environment>::PairingCurve, VarunaHidingMode>>>

source§

impl Deref for CREDITS_VERIFYING_KEYS

§

type Target = IndexMap<String, Arc<CircuitVerifyingKey<<Console as Environment>::PairingCurve>>>

source§

impl Deref for ENCRYPTION_DOMAIN

§

type Target = Field<Testnet3>

source§

impl Deref for GENERATOR_G

§

type Target = Vec<Group<Testnet3>>

source§

impl Deref for GRAPH_KEY_DOMAIN

§

type Target = Field<Testnet3>

source§

impl Deref for PEDERSEN_64

§

type Target = Pedersen<Testnet3, 64>

source§

impl Deref for PEDERSEN_128

§

type Target = Pedersen<Testnet3, 128>

source§

impl Deref for POSEIDON_2

§

type Target = Poseidon<Testnet3, 2>

source§

impl Deref for POSEIDON_4

§

type Target = Poseidon<Testnet3, 4>

source§

impl Deref for POSEIDON_8

§

type Target = Poseidon<Testnet3, 8>

source§

impl Deref for SERIAL_NUMBER_DOMAIN

§

type Target = Field<Testnet3>

source§

impl Deref for VARUNA_FS_PARAMETERS

§

type Target = <PoseidonSponge<<<Testnet3 as Environment>::PairingCurve as PairingEngine>::Fq, 2, 1> as AlgebraicSponge<<<Testnet3 as Environment>::PairingCurve as PairingEngine>::Fq, 2>>::Parameters

source§

impl Deref for Error

§

type Target = dyn Error + Sync + Send

§

impl Deref for ColoredString

§

type Target = str

§

impl Deref for INCLUSION_PROVING_KEY

§

type Target = Vec<u8>

§

impl Deref for INCLUSION_VERIFYING_KEY

§

type Target = Vec<u8>

§

impl Deref for SHOULD_COLORIZE

§

type Target = ShouldColorize

§

impl Deref for SmolStr

§

type Target = str

1.36.0 · source§

impl<'a> Deref for IoSlice<'a>

§

type Target = [u8]

1.36.0 · source§

impl<'a> Deref for IoSliceMut<'a>

§

type Target = [u8]

source§

impl<'a> Deref for MaybeUninitSlice<'a>

source§

impl<'a, 'f> Deref for VaList<'a, 'f>where 'f: 'a,

§

type Target = VaListImpl<'f>

§

impl<'a, R, G, T> Deref for MappedReentrantMutexGuard<'a, R, G, T>where R: RawMutex + 'a, G: GetThreadId + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, G, T> Deref for ReentrantMutexGuard<'a, R, G, T>where R: RawMutex + 'a, G: GetThreadId + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for MappedMutexGuard<'a, R, T>where R: RawMutex + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for MappedRwLockReadGuard<'a, R, T>where R: RawRwLock + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for MappedRwLockWriteGuard<'a, R, T>where R: RawRwLock + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for MutexGuard<'a, R, T>where R: RawMutex + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for RwLockReadGuard<'a, R, T>where R: RawRwLock + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for RwLockUpgradableReadGuard<'a, R, T>where R: RawRwLockUpgrade + 'a, T: 'a + ?Sized,

§

type Target = T

§

impl<'a, R, T> Deref for RwLockWriteGuard<'a, R, T>where R: RawRwLock + 'a, T: 'a + ?Sized,

§

type Target = T

source§

impl<'s> Deref for SockRef<'s>

§

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

§

type Target = [<A as Array>::Item]

source§

impl<B> Deref for Cow<'_, B>where B: ToOwned + ?Sized, <B as ToOwned>::Owned: Borrow<B>,

§

type Target = B

§

impl<B, T> Deref for Ref<B, [T]>where B: ByteSlice, T: FromBytes,

§

type Target = [T]

§

impl<B, T> Deref for Ref<B, T>where B: ByteSlice, T: FromBytes,

§

type Target = T

§

impl<E> Deref for Boolean<E>where E: Environment,

§

type Target = bool

§

impl<E> Deref for Field<E>where E: Environment,

§

type Target = <E as Environment>::Field

§

impl<E> Deref for Group<E>where E: Environment,

§

type Target = <E as Environment>::Projective

§

impl<E> Deref for Scalar<E>where E: Environment,

§

type Target = <E as Environment>::Scalar

§

impl<E, I> Deref for Integer<E, I>where E: Environment, I: IntegerType,

§

type Target = I

source§

impl<F> Deref for DensePolynomial<F>where F: Field,

§

type Target = [F]

source§

impl<F> Deref for LabeledPolynomial<F>where F: Field,

§

type Target = Polynomial<'static, F>

source§

impl<F, const PREFIX: u16> Deref for AleoID<F, PREFIX>where F: FieldTrait,

§

type Target = F

source§

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

§

type Target = <L as Deref>::Target

1.33.0 · source§

impl<P> Deref for Pin<P>where P: Deref,

§

type Target = <P as Deref>::Target

source§

impl<T> Deref for &Twhere T: ?Sized,

§

type Target = T

source§

impl<T> Deref for &mut Twhere T: ?Sized,

§

type Target = T

source§

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

§

type Target = T

source§

impl<T> Deref for UniqueRc<T>

§

type Target = T

source§

impl<T> Deref for core::cell::Ref<'_, T>where T: ?Sized,

§

type Target = T

source§

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

§

type Target = T

1.20.0 · source§

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

§

type Target = T

1.9.0 · source§

impl<T> Deref for AssertUnwindSafe<T>

§

type Target = T

source§

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

§

type Target = T

source§

impl<T> Deref for std::sync::rwlock::RwLockReadGuard<'_, T>where T: ?Sized,

§

type Target = T

source§

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

§

type Target = T

§

impl<T> Deref for CachePadded<T>

§

type Target = T

§

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

§

type Target = T

§

impl<T> Deref for ShardedLockReadGuard<'_, T>where T: ?Sized,

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

source§

impl<T, A> Deref for alloc::boxed::Box<T, A>where A: Allocator, T: ?Sized,

§

type Target = T

1.12.0 · source§

impl<T, A> Deref for PeekMut<'_, T, A>where T: Ord, A: Allocator,

§

type Target = T

source§

impl<T, A> Deref for Rc<T, A>where A: Allocator, T: ?Sized,

§

type Target = T

source§

impl<T, A> Deref for Arc<T, A>where A: Allocator, T: ?Sized,

§

type Target = T

source§

impl<T, A> Deref for alloc::vec::Vec<T, A>where A: Allocator,

§

type Target = [T]

§

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

§

type Target = T

§

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

§

type Target = [T]

source§

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

§

type Target = T

source§

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

§

type Target = T

source§

impl<T, F> Deref for once_cell::sync::Lazy<T, F>where F: FnOnce() -> T,

§

type Target = T

source§

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

§

type Target = T

source§

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

§

type Target = T

§

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

§

type Target = [T]

source§

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

§

type Target = [T]

source§

impl<T, const PREFIX: u32> Deref for AleoObject<T, PREFIX>where T: Clone + Debug + ToBytes + FromBytes + PartialEq + Eq + Sync + Send,

§

type Target = T

§

impl<Z> Deref for Zeroizing<Z>where Z: Zeroize,

§

type Target = Z

source§

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

§

type Target = str

§

impl<const VARIANT: usize> Deref for BooleanHash<VARIANT>