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
(whereT
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 typeU
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:
- 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 BuildArtifactTypeId
impl Deref for BuildArtifactTypeId
type Target = BuildArtifactType
Sourceยงimpl Deref for BuildTargetId
impl Deref for BuildTargetId
type Target = BuildTarget
Sourceยงimpl Deref for FLAMSExtensionId
impl Deref for FLAMSExtensionId
type Target = FLAMSExtension
Sourceยงimpl Deref for SourceFormatId
impl Deref for SourceFormatId
type Target = SourceFormat
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
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
Sourceยงimpl Deref for X509VerifyParam
impl Deref for X509VerifyParam
type Target = X509VerifyParamRef
Sourceยงimpl Deref for AbortSignal
impl Deref for AbortSignal
type Target = EventTarget
Sourceยงimpl Deref for EventSource
impl Deref for EventSource
type Target = EventTarget
Sourceยงimpl Deref for FileReader
impl Deref for FileReader
type Target = EventTarget
Sourceยงimpl Deref for HtmlAnchorElement
impl Deref for HtmlAnchorElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlAreaElement
impl Deref for HtmlAreaElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlAudioElement
impl Deref for HtmlAudioElement
type Target = HtmlMediaElement
Sourceยงimpl Deref for HtmlBaseElement
impl Deref for HtmlBaseElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlBodyElement
impl Deref for HtmlBodyElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlBrElement
impl Deref for HtmlBrElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlButtonElement
impl Deref for HtmlButtonElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlCanvasElement
impl Deref for HtmlCanvasElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDListElement
impl Deref for HtmlDListElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDataElement
impl Deref for HtmlDataElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDataListElement
impl Deref for HtmlDataListElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDetailsElement
impl Deref for HtmlDetailsElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDialogElement
impl Deref for HtmlDialogElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlDivElement
impl Deref for HtmlDivElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlEmbedElement
impl Deref for HtmlEmbedElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlFieldSetElement
impl Deref for HtmlFieldSetElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlFormElement
impl Deref for HtmlFormElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlHeadElement
impl Deref for HtmlHeadElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlHeadingElement
impl Deref for HtmlHeadingElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlHrElement
impl Deref for HtmlHrElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlHtmlElement
impl Deref for HtmlHtmlElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlIFrameElement
impl Deref for HtmlIFrameElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlImageElement
impl Deref for HtmlImageElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlInputElement
impl Deref for HtmlInputElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlLabelElement
impl Deref for HtmlLabelElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlLegendElement
impl Deref for HtmlLegendElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlLiElement
impl Deref for HtmlLiElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlLinkElement
impl Deref for HtmlLinkElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlMapElement
impl Deref for HtmlMapElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlMediaElement
impl Deref for HtmlMediaElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlMenuElement
impl Deref for HtmlMenuElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlMetaElement
impl Deref for HtmlMetaElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlMeterElement
impl Deref for HtmlMeterElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlModElement
impl Deref for HtmlModElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlOListElement
impl Deref for HtmlOListElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlObjectElement
impl Deref for HtmlObjectElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlOptGroupElement
impl Deref for HtmlOptGroupElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlOptionElement
impl Deref for HtmlOptionElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlOutputElement
impl Deref for HtmlOutputElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlParagraphElement
impl Deref for HtmlParagraphElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlParamElement
impl Deref for HtmlParamElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlPictureElement
impl Deref for HtmlPictureElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlPreElement
impl Deref for HtmlPreElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlProgressElement
impl Deref for HtmlProgressElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlQuoteElement
impl Deref for HtmlQuoteElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlScriptElement
impl Deref for HtmlScriptElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlSelectElement
impl Deref for HtmlSelectElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlSlotElement
impl Deref for HtmlSlotElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlSourceElement
impl Deref for HtmlSourceElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlSpanElement
impl Deref for HtmlSpanElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlStyleElement
impl Deref for HtmlStyleElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableCaptionElement
impl Deref for HtmlTableCaptionElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableCellElement
impl Deref for HtmlTableCellElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableColElement
impl Deref for HtmlTableColElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableElement
impl Deref for HtmlTableElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableRowElement
impl Deref for HtmlTableRowElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTableSectionElement
impl Deref for HtmlTableSectionElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTemplateElement
impl Deref for HtmlTemplateElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTextAreaElement
impl Deref for HtmlTextAreaElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTimeElement
impl Deref for HtmlTimeElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTitleElement
impl Deref for HtmlTitleElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlTrackElement
impl Deref for HtmlTrackElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlUListElement
impl Deref for HtmlUListElement
type Target = HtmlElement
Sourceยงimpl Deref for HtmlVideoElement
impl Deref for HtmlVideoElement
type Target = HtmlMediaElement
Sourceยงimpl Deref for PointerEvent
impl Deref for PointerEvent
type Target = MouseEvent
Sourceยงimpl Deref for ShadowRoot
impl Deref for ShadowRoot
type Target = DocumentFragment
Sourceยงimpl Deref for WheelEvent
impl Deref for WheelEvent
type Target = MouseEvent
ยงimpl Deref for HtmlElement
impl Deref for HtmlElement
type Target = HtmlElement
ยงimpl<'a, R, G, T> Deref for MappedReentrantMutexGuard<'a, R, G, T>where
R: RawMutex + 'a,
G: GetThreadId + 'a,
T: 'a + ?Sized,
impl<'a, R, G, T> Deref for MappedReentrantMutexGuard<'a, R, G, T>where
R: RawMutex + 'a,
G: GetThreadId + 'a,
T: 'a + ?Sized,
ยงimpl<'a, R, G, T> Deref for ReentrantMutexGuard<'a, R, G, T>where
R: RawMutex + 'a,
G: GetThreadId + 'a,
T: 'a + ?Sized,
impl<'a, R, G, T> Deref for ReentrantMutexGuard<'a, R, G, T>where
R: RawMutex + 'a,
G: GetThreadId + 'a,
T: 'a + ?Sized,
ยง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, T, U> Deref for FromColorMutGuard<'a, T, U>
impl<'a, T, U> Deref for FromColorMutGuard<'a, T, U>
Sourceยงimpl<'a, T, U> Deref for FromColorUnclampedMutGuard<'a, T, U>
impl<'a, T, U> Deref for FromColorUnclampedMutGuard<'a, T, U>
ยงimpl<B, T> Deref for Ref<B, T>where
B: ByteSlice,
T: FromBytes + KnownLayout + Immutable + ?Sized,
impl<B, T> Deref for Ref<B, T>where
B: ByteSlice,
T: FromBytes + KnownLayout + Immutable + ?Sized,
ยงimpl<S> Deref for ArcServerAction<S>
impl<S> Deref for ArcServerAction<S>
ยงimpl<S> Deref for ArcServerMultiAction<S>
impl<S> Deref for ArcServerMultiAction<S>
ยงimpl<S> Deref for ServerAction<S>
impl<S> Deref for ServerAction<S>
ยงimpl<S> Deref for ServerMultiAction<S>
impl<S> Deref for ServerMultiAction<S>
ยงimpl<S, G> Deref for WriteGuard<S, G>
impl<S, G> Deref for WriteGuard<S, G>
ยงimpl<T> Deref for AsyncPlain<T>
impl<T> Deref for AsyncPlain<T>
ยงimpl<T> Deref for UntrackedWriteGuard<T>
impl<T> Deref for UntrackedWriteGuard<T>
ยงimpl<T> Deref for ArcLocalResource<T>
impl<T> Deref for ArcLocalResource<T>
type Target = ArcAsyncDerived<T>
ยงimpl<T> Deref for DoubleDeref<T>
impl<T> Deref for DoubleDeref<T>
ยงimpl<T> Deref for LocalResource<T>
impl<T> Deref for LocalResource<T>
type Target = AsyncDerived<T>
ยงimpl<T> Deref for ConnectInfo<T>
impl<T> Deref for ConnectInfo<T>
1.0.0 (const: unstable) ยท Sourceยงimpl<T> Deref for flams_router_vscode::server_fn::inventory::core::cell::Ref<'_, T>where
T: ?Sized,
impl<T> Deref for flams_router_vscode::server_fn::inventory::core::cell::Ref<'_, T>where
T: ?Sized,
1.0.0 (const: unstable) ยท Sourceยงimpl<T> Deref for flams_router_vscode::server_fn::inventory::core::cell::RefMut<'_, T>where
T: ?Sized,
impl<T> Deref for flams_router_vscode::server_fn::inventory::core::cell::RefMut<'_, T>where
T: ?Sized,
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::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 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>
ยง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.