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 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).
§Deref coercion
If T implements Deref<Target = U>, and v is a value of type T, then:
- In immutable contexts,
*v(whereTis neither a reference nor a raw pointer) is equivalent to*Deref::deref(&v). - Values of type
&Tare coerced to values of type&U Timplicitly implements all the methods of the typeUwhich take the&selfreceiver.
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:
- a value of the type transparently behaves like a value of the target type;
- the implementation of the deref function is cheap; and
- users of the type will not be surprised by any deref coercion behavior.
In general, deref traits should not be implemented if:
- the deref implementations could fail unexpectedly; or
- the type has methods that are likely to collide with methods on the target type; or
- 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 coercion 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§
Required Methods§
Implementors§
Source§impl Deref for rustls::conn::connection::Connection
impl Deref for rustls::conn::connection::Connection
type Target = CommonState
Source§impl Deref for rustls::quic::connection::Connection
impl Deref for rustls::quic::connection::Connection
type Target = CommonState
Source§impl Deref for SHOULD_COLORIZE
impl Deref for SHOULD_COLORIZE
type Target = ShouldColorize
Source§impl Deref for CurrencyType
impl Deref for CurrencyType
type Target = TinyAsciiStr<3>
Source§impl Deref for RegionOverride
impl Deref for RegionOverride
type Target = SubdivisionId
Source§impl Deref for RegionalSubdivision
impl Deref for RegionalSubdivision
type Target = SubdivisionId
Source§impl Deref for Asn1BitString
impl Deref for Asn1BitString
type Target = Asn1BitStringRef
Source§impl Deref for Asn1Enumerated
impl Deref for Asn1Enumerated
type Target = Asn1EnumeratedRef
Source§impl Deref for Asn1GeneralizedTime
impl Deref for Asn1GeneralizedTime
Source§impl Deref for Asn1Integer
impl Deref for Asn1Integer
type Target = Asn1IntegerRef
Source§impl Deref for Asn1Object
impl Deref for Asn1Object
type Target = Asn1ObjectRef
Source§impl Deref for Asn1OctetString
impl Deref for Asn1OctetString
type Target = Asn1OctetStringRef
Source§impl Deref for Asn1String
impl Deref for Asn1String
type Target = Asn1StringRef
Source§impl Deref for BigNumContext
impl Deref for BigNumContext
type Target = BigNumContextRef
Source§impl Deref for CmsContentInfo
impl Deref for CmsContentInfo
type Target = CmsContentInfoRef
Source§impl Deref for OcspBasicResponse
impl Deref for OcspBasicResponse
Source§impl Deref for OcspCertId
impl Deref for OcspCertId
type Target = OcspCertIdRef
Source§impl Deref for OcspOneReq
impl Deref for OcspOneReq
type Target = OcspOneReqRef
Source§impl Deref for OcspRequest
impl Deref for OcspRequest
type Target = OcspRequestRef
Source§impl Deref for OcspResponse
impl Deref for OcspResponse
type Target = OcspResponseRef
Source§impl Deref for Pkcs7Signed
impl Deref for Pkcs7Signed
type Target = Pkcs7SignedRef
Source§impl Deref for Pkcs7SignerInfo
impl Deref for Pkcs7SignerInfo
type Target = Pkcs7SignerInfoRef
Source§impl Deref for SrtpProtectionProfile
impl Deref for SrtpProtectionProfile
Source§impl Deref for SslAcceptorBuilder
impl Deref for SslAcceptorBuilder
type Target = SslContextBuilder
Source§impl Deref for SslConnectorBuilder
impl Deref for SslConnectorBuilder
type Target = SslContextBuilder
Source§impl Deref for SslContext
impl Deref for SslContext
type Target = SslContextRef
Source§impl Deref for SslSession
impl Deref for SslSession
type Target = SslSessionRef
Source§impl Deref for OpensslString
impl Deref for OpensslString
type Target = OpensslStringRef
Source§impl Deref for X509StoreBuilder
impl Deref for X509StoreBuilder
type Target = X509StoreBuilderRef
Source§impl Deref for AccessDescription
impl Deref for AccessDescription
Source§impl Deref for DistPointName
impl Deref for DistPointName
type Target = DistPointNameRef
Source§impl Deref for GeneralName
impl Deref for GeneralName
type Target = GeneralNameRef
Source§impl Deref for X509Algorithm
impl Deref for X509Algorithm
type Target = X509AlgorithmRef
Source§impl Deref for X509Extension
impl Deref for X509Extension
type Target = X509ExtensionRef
Source§impl Deref for X509NameEntry
impl Deref for X509NameEntry
type Target = X509NameEntryRef
Source§impl Deref for X509Object
impl Deref for X509Object
type Target = X509ObjectRef
Source§impl Deref for X509Revoked
impl Deref for X509Revoked
type Target = X509RevokedRef
Source§impl Deref for X509StoreContext
impl Deref for X509StoreContext
type Target = X509StoreContextRef
Source§impl Deref for X509VerifyParam
impl Deref for X509VerifyParam
type Target = X509VerifyParamRef
Source§impl Deref for rustls::client::client_conn::connection::ClientConnection
impl Deref for rustls::client::client_conn::connection::ClientConnection
Source§impl Deref for UnbufferedClientConnection
impl Deref for UnbufferedClientConnection
Source§impl Deref for rustls::quic::connection::ClientConnection
impl Deref for rustls::quic::connection::ClientConnection
Source§impl Deref for rustls::quic::connection::ServerConnection
impl Deref for rustls::quic::connection::ServerConnection
Source§impl Deref for rustls::server::server_conn::connection::ServerConnection
impl Deref for rustls::server::server_conn::connection::ServerConnection
Source§impl Deref for UnbufferedServerConnection
impl Deref for UnbufferedServerConnection
Source§impl Deref for CREDITS_PROVING_KEYS
impl Deref for CREDITS_PROVING_KEYS
type Target = IndexMap<String, Arc<CircuitProvingKey<<Console as Environment>::PairingCurve, VarunaHidingMode>>>
Source§impl Deref for CREDITS_VERIFYING_KEYS
impl Deref for CREDITS_VERIFYING_KEYS
type Target = IndexMap<String, Arc<CircuitVerifyingKey<<Console as Environment>::PairingCurve>>>
Source§impl Deref for VARUNA_FS_PARAMETERS
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<'a> Deref for socket2::MaybeUninitSlice<'a>
impl<'a> Deref for socket2::MaybeUninitSlice<'a>
type Target = [MaybeUninit<u8>]
Source§impl<'a> Deref for socket2::MaybeUninitSlice<'a>
impl<'a> Deref for socket2::MaybeUninitSlice<'a>
type Target = [MaybeUninit<u8>]
Source§impl<'a, R, G, T> Deref for MappedReentrantMutexGuard<'a, R, G, T>
impl<'a, R, G, T> Deref for MappedReentrantMutexGuard<'a, R, G, T>
Source§impl<'a, R, G, T> Deref for ReentrantMutexGuard<'a, R, G, T>
impl<'a, R, G, T> Deref for ReentrantMutexGuard<'a, R, G, T>
Source§impl<'a, R, T> Deref for lock_api::mutex::MappedMutexGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::mutex::MappedMutexGuard<'a, R, T>
Source§impl<'a, R, T> Deref for lock_api::mutex::MutexGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::mutex::MutexGuard<'a, R, T>
Source§impl<'a, R, T> Deref for lock_api::rwlock::MappedRwLockReadGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::rwlock::MappedRwLockReadGuard<'a, R, T>
Source§impl<'a, R, T> Deref for lock_api::rwlock::MappedRwLockWriteGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::rwlock::MappedRwLockWriteGuard<'a, R, T>
Source§impl<'a, R, T> Deref for lock_api::rwlock::RwLockReadGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::rwlock::RwLockReadGuard<'a, R, T>
Source§impl<'a, R, T> Deref for RwLockUpgradableReadGuard<'a, R, T>where
R: RawRwLockUpgrade + 'a,
T: 'a + ?Sized,
impl<'a, R, T> Deref for RwLockUpgradableReadGuard<'a, R, T>where
R: RawRwLockUpgrade + 'a,
T: 'a + ?Sized,
Source§impl<'a, R, T> Deref for lock_api::rwlock::RwLockWriteGuard<'a, R, T>
impl<'a, R, T> Deref for lock_api::rwlock::RwLockWriteGuard<'a, R, T>
Source§impl<'a, T> Deref for tokio::sync::mutex::MappedMutexGuard<'a, T>where
T: ?Sized,
impl<'a, T> Deref for tokio::sync::mutex::MappedMutexGuard<'a, T>where
T: ?Sized,
Source§impl<A> Deref for snarkvm_circuit_program::data::record::helpers::owner::Owner<A, Plaintext<A>>where
A: Aleo,
impl<A> Deref for snarkvm_circuit_program::data::record::helpers::owner::Owner<A, Plaintext<A>>where
A: Aleo,
Source§impl<A> Deref for snarkvm_circuit_program::data::ciphertext::Ciphertext<A>where
A: Aleo,
impl<A> Deref for snarkvm_circuit_program::data::ciphertext::Ciphertext<A>where
A: Aleo,
Source§impl<C0, C1, T> Deref for EitherCart<C0, C1>
impl<C0, C1, T> Deref for EitherCart<C0, C1>
Source§impl<Data> Deref for rustls::quic::connection::ConnectionCommon<Data>
impl<Data> Deref for rustls::quic::connection::ConnectionCommon<Data>
type Target = CommonState
Source§impl<E> Deref for snarkvm_circuit_types_boolean::Boolean<E>where
E: Environment,
impl<E> Deref for snarkvm_circuit_types_boolean::Boolean<E>where
E: Environment,
type Target = LinearCombination<<E as Environment>::BaseField>
Source§impl<E> Deref for snarkvm_debug::prelude::Boolean<E>where
E: Environment,
impl<E> Deref for snarkvm_debug::prelude::Boolean<E>where
E: Environment,
Source§impl<E> Deref for Field<E>where
E: Environment,
impl<E> Deref for Field<E>where
E: Environment,
type Target = <E as Environment>::Field
Source§impl<E> Deref for Group<E>where
E: Environment,
impl<E> Deref for Group<E>where
E: Environment,
type Target = <E as Environment>::Projective
Source§impl<E> Deref for Scalar<E>where
E: Environment,
impl<E> Deref for Scalar<E>where
E: Environment,
type Target = <E as Environment>::Scalar
Source§impl<E> Deref for StringType<E>where
E: Environment,
impl<E> Deref for StringType<E>where
E: Environment,
Source§impl<E, I> Deref for Integer<E, I>where
E: Environment,
I: IntegerType,
impl<E, I> Deref for Integer<E, I>where
E: Environment,
I: IntegerType,
Source§impl<E, const VARIANT: usize> Deref for snarkvm_circuit_collections::kary_merkle_tree::helpers::BooleanHash<E, VARIANT>where
E: Environment,
impl<E, const VARIANT: usize> Deref for snarkvm_circuit_collections::kary_merkle_tree::helpers::BooleanHash<E, VARIANT>where
E: Environment,
Source§impl<F> Deref for LabeledPolynomial<F>where
F: Field,
impl<F> Deref for LabeledPolynomial<F>where
F: Field,
type Target = Polynomial<'static, F>
Source§impl<N> Deref for ConfirmedTransaction<N>where
N: Network,
impl<N> Deref for ConfirmedTransaction<N>where
N: Network,
type Target = Transaction<N>
Source§impl<N> Deref for CoinbaseSolution<N>where
N: Network,
impl<N> Deref for CoinbaseSolution<N>where
N: Network,
type Target = IndexMap<PuzzleCommitment<N>, ProverSolution<N>>
Source§impl<N> Deref for PuzzleCommitment<N>where
N: Network,
impl<N> Deref for PuzzleCommitment<N>where
N: Network,
type Target = KZGCommitment<<N as Environment>::PairingCurve>
Source§impl<N> Deref for Certificate<N>where
N: Network,
impl<N> Deref for Certificate<N>where
N: Network,
type Target = Certificate<<N as Environment>::PairingCurve>
Source§impl<N> Deref for snarkvm_debug::prelude::Ciphertext<N>where
N: Network,
impl<N> Deref for snarkvm_debug::prelude::Ciphertext<N>where
N: Network,
Source§impl<N> Deref for Proof<N>where
N: Network,
impl<N> Deref for Proof<N>where
N: Network,
type Target = Proof<<N as Environment>::PairingCurve>
Source§impl<N> Deref for ProvingKey<N>where
N: Network,
impl<N> Deref for ProvingKey<N>where
N: Network,
type Target = CircuitProvingKey<<N as Environment>::PairingCurve, VarunaHidingMode>
Source§impl<N> Deref for UniversalSRS<N>where
N: Network,
impl<N> Deref for UniversalSRS<N>where
N: Network,
type Target = UniversalParams<<N as Environment>::PairingCurve>
Source§impl<N> Deref for VerifyingKey<N>where
N: Network,
impl<N> Deref for VerifyingKey<N>where
N: Network,
type Target = CircuitVerifyingKey<<N as Environment>::PairingCurve>
Source§impl<T> Deref for std::sync::nonpoison::mutex::MappedMutexGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::mutex::MappedMutexGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::nonpoison::mutex::MutexGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::mutex::MutexGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::nonpoison::rwlock::MappedRwLockReadGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::rwlock::MappedRwLockReadGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::nonpoison::rwlock::MappedRwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::rwlock::MappedRwLockWriteGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::nonpoison::rwlock::RwLockReadGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::rwlock::RwLockReadGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::nonpoison::rwlock::RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::nonpoison::rwlock::RwLockWriteGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::poison::mutex::MappedMutexGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::mutex::MappedMutexGuard<'_, T>where
T: ?Sized,
1.0.0 · Source§impl<T> Deref for std::sync::poison::mutex::MutexGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::mutex::MutexGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::poison::rwlock::MappedRwLockReadGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::rwlock::MappedRwLockReadGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for std::sync::poison::rwlock::MappedRwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::rwlock::MappedRwLockWriteGuard<'_, T>where
T: ?Sized,
1.0.0 · Source§impl<T> Deref for std::sync::poison::rwlock::RwLockReadGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::rwlock::RwLockReadGuard<'_, T>where
T: ?Sized,
1.0.0 · Source§impl<T> Deref for std::sync::poison::rwlock::RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Deref for std::sync::poison::rwlock::RwLockWriteGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for futures_util::lock::mutex::MutexGuard<'_, T>where
T: ?Sized,
impl<T> Deref for futures_util::lock::mutex::MutexGuard<'_, T>where
T: ?Sized,
Source§impl<T> Deref for futures_util::lock::mutex::OwnedMutexGuard<T>where
T: ?Sized,
impl<T> Deref for futures_util::lock::mutex::OwnedMutexGuard<T>where
T: ?Sized,
Source§impl<T> Deref for X509Lookup<T>
impl<T> Deref for X509Lookup<T>
type Target = X509LookupRef<T>
Source§impl<T> Deref for X509LookupMethod<T>
impl<T> Deref for X509LookupMethod<T>
type Target = X509LookupMethodRef<T>
Source§impl<T> Deref for Metadata<'_, T>where
T: SmartDisplay + ?Sized,
Permit using Metadata as a smart pointer to the user-provided metadata.
impl<T> Deref for Metadata<'_, T>where
T: SmartDisplay + ?Sized,
Permit using Metadata as a smart pointer to the user-provided metadata.