Trait Deref

1.0.0 (const: unstable) · Source
pub trait Deref {
    type Target: ?Sized;

    // Required method
    const 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 (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 behavior.

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 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§

1.0.0 · Source

type Target: ?Sized

The resulting type after dereferencing.

Required Methods§

1.0.0 · Source

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

Dereferences the value.

Implementors§

Source§

impl Deref for MultiResourcePoolStateFieldPayload

Source§

impl Deref for OneResourcePoolStateFieldPayload

Source§

impl Deref for TwoResourcePoolStateFieldPayload

Source§

impl Deref for ACCESS_CONTROLLER_PACKAGE_DEFINITION_V1_0

Source§

impl Deref for ACCESS_CONTROLLER_PACKAGE_DEFINITION_V2_0

Source§

impl Deref for ACCOUNT_PACKAGE_DEFINITION

Source§

impl Deref for CONSENSUS_MANAGER_PACKAGE_DEFINITION

Source§

impl Deref for ConsensusManagerConfigurationFieldPayload

Source§

impl Deref for ConsensusManagerCurrentProposalStatisticFieldPayload

Source§

impl Deref for ConsensusManagerCurrentValidatorSetFieldPayload

Source§

impl Deref for ConsensusManagerProposerMilliTimestampFieldPayload

Source§

impl Deref for ConsensusManagerProposerMinuteTimestampFieldPayload

Source§

impl Deref for ConsensusManagerRegisteredValidatorByStakeEntryPayload

Source§

impl Deref for ConsensusManagerStateFieldPayload

Source§

impl Deref for ConsensusManagerValidatorRewardsFieldPayload

Source§

impl Deref for DEFAULT_TESTING_FAUCET_SUPPLY

Source§

impl Deref for DEFAULT_VALIDATOR_USD_COST

Source§

impl Deref for DEFAULT_VALIDATOR_XRD_COST

Source§

impl Deref for FungibleResourceManagerDivisibilityFieldPayload

Source§

impl Deref for FungibleResourceManagerTotalSupplyFieldPayload

Source§

impl Deref for FungibleVaultBalanceFieldPayload

Source§

impl Deref for FungibleVaultFreezeStatusFieldPayload

Source§

impl Deref for FungibleVaultLockedBalanceFieldPayload

Source§

impl Deref for IDENTITY_PACKAGE_DEFINITION

Source§

impl Deref for LOCKER_PACKAGE_DEFINITION

Source§

impl Deref for METADATA_PACKAGE_DEFINITION

Source§

impl Deref for NATIVE_FUNCTION_BASE_COSTS

Source§

impl Deref for NATIVE_FUNCTION_BASE_COSTS_SIZE_DEPENDENT

Source§

impl Deref for NonFungibleResourceManagerIdTypeFieldPayload

Source§

impl Deref for NonFungibleResourceManagerMutableFieldsFieldPayload

Source§

impl Deref for NonFungibleResourceManagerTotalSupplyFieldPayload

Source§

impl Deref for NonFungibleVaultBalanceFieldPayload

Source§

impl Deref for NonFungibleVaultFreezeStatusFieldPayload

Source§

impl Deref for NonFungibleVaultLockedResourceFieldPayload

Source§

impl Deref for NonFungibleVaultNonFungibleEntryPayload

Source§

impl Deref for PACKAGE_PACKAGE_DEFINITION

Source§

impl Deref for POOL_PACKAGE_DEFINITION_V1_0

Source§

impl Deref for POOL_PACKAGE_DEFINITION_V1_1

Source§

impl Deref for PackageBlueprintVersionAuthConfigEntryPayload

Source§

impl Deref for PackageBlueprintVersionDefinitionEntryPayload

Source§

impl Deref for PackageBlueprintVersionDependenciesEntryPayload

Source§

impl Deref for PackageBlueprintVersionRoyaltyConfigEntryPayload

Source§

impl Deref for PackageCodeInstrumentedCodeEntryPayload

Source§

impl Deref for PackageCodeOriginalCodeEntryPayload

Source§

impl Deref for PackageCodeVmTypeEntryPayload

Source§

impl Deref for PackageRoyaltyAccumulatorFieldPayload

Source§

impl Deref for PackageSchemaEntryPayload

Source§

impl Deref for RESOURCE_PACKAGE_DEFINITION

Source§

impl Deref for ROLE_ASSIGNMENT_PACKAGE_DEFINITION

Source§

impl Deref for ROYALTY_PACKAGE_DEFINITION

1.0.0 · Source§

impl Deref for String

Source§

impl Deref for TRANSACTION_PROCESSOR_PACKAGE_DEFINITION

Source§

impl Deref for TRANSACTION_TRACKER_PACKAGE_DEFINITION

Source§

impl Deref for ValidatorProtocolUpdateReadinessSignalFieldPayload

Source§

impl Deref for ValidatorStateFieldPayload

Source§

impl Deref for AccessControllerStateFieldPayload

Source§

impl Deref for AccessControllerV2StateFieldPayload

Source§

impl Deref for ByteString

1.0.0 · Source§

impl Deref for CString

Source§

impl Deref for ByteStr

1.0.0 · Source§

impl Deref for std::ffi::os_str::OsString

1.0.0 · Source§

impl Deref for PathBuf

Source§

impl Deref for AliasableString

Source§

impl Deref for Error

Source§

type Target = dyn Error + Send + Sync

Source§

impl Deref for SHOULD_COLORIZE

Source§

impl Deref for ColoredString

Source§

impl Deref for fslock::unix::OsString

Source§

impl Deref for AccountAuthorizedDepositorEntryPayload

Source§

impl Deref for AccountDepositRuleFieldPayload

Source§

impl Deref for AccountResourcePreferenceEntryPayload

Source§

impl Deref for AccountResourceVaultEntryPayload

Source§

impl Deref for AccountLockerAccountClaimsEntryPayload

Source§

impl Deref for MetadataEntryEntryPayload

Source§

impl Deref for RoleAssignmentAccessRuleEntryPayload

Source§

impl Deref for RoleAssignmentOwnerFieldPayload

Source§

impl Deref for ComponentRoyaltyAccumulatorFieldPayload

Source§

impl Deref for ComponentRoyaltyMethodAmountEntryPayload

Source§

impl Deref for KNOWN_ENUM_DISCRIMINATORS

Source§

type Target = NonIterMap<&'static str, u8>

Source§

impl Deref for FungibleResourceManager

Source§

impl Deref for NonFungibleResourceManager

Source§

impl Deref for ResourceManager

Source§

impl Deref for SerializedSignature

Source§

impl Deref for BuildMetadata

Source§

impl Deref for Prerelease

Source§

impl Deref for Add

Source§

type Target = WithSpan

Source§

impl Deref for And

Source§

type Target = WithSpan

Source§

impl Deref for At

Source§

type Target = WithSpan

Source§

impl Deref for Bang

Source§

type Target = WithSpan

Source§

impl Deref for Caret

Source§

type Target = WithSpan

Source§

impl Deref for Colon

Source§

type Target = WithSpan

Source§

impl Deref for Comma

Source§

type Target = WithSpan

Source§

impl Deref for Div

Source§

type Target = WithSpan

Source§

impl Deref for Dollar

Source§

type Target = WithSpan

Source§

impl Deref for Dot

Source§

type Target = WithSpan

Source§

impl Deref for Eq

Source§

type Target = WithSpan

Source§

impl Deref for Gt

Source§

type Target = WithSpan

Source§

impl Deref for Lt

Source§

type Target = WithSpan

Source§

impl Deref for Or

Source§

type Target = WithSpan

Source§

impl Deref for Pound

Source§

type Target = WithSpan

Source§

impl Deref for Question

Source§

type Target = WithSpan

Source§

impl Deref for Rem

Source§

type Target = WithSpan

Source§

impl Deref for Semi

Source§

type Target = WithSpan

Source§

impl Deref for Star

Source§

type Target = WithSpan

Source§

impl Deref for Sub

Source§

type Target = WithSpan

Source§

impl Deref for Tilde

Source§

type Target = WithSpan

Source§

impl Deref for Underscore

Source§

type Target = WithSpan

Source§

impl Deref for TempPath

Source§

impl Deref for Document

Source§

impl Deref for InternalString

Source§

impl Deref for Key

Source§

impl Deref for wasmparser_nostd::validator::types::KebabStr

Source§

impl Deref for wasmparser_nostd::validator::types::KebabString

Source§

impl Deref for wasmparser::validator::names::KebabStr

Source§

impl Deref for wasmparser::validator::names::KebabString

Source§

impl Deref for BStr

Source§

impl Deref for Bytes

1.36.0 · Source§

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

1.36.0 · Source§

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

Source§

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

Source§

impl<'a, O> Deref for Attached<'a, O>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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>
where R: RawRwLock + 'a, T: 'a + ?Sized,

Source§

impl<'a, T> Deref for SpinMutexGuard<'a, T>
where T: ?Sized,

Source§

impl<'a, T> Deref for spin::mutex::MutexGuard<'a, T>
where T: ?Sized,

Source§

impl<'a, T> Deref for ArcBorrow<'a, T>

Source§

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

Source§

impl<'a, V> Deref for KeyValueEntryRef<'a, V>
where V: ScryptoEncode,

Source§

impl<'a, V> Deref for KeyValueEntryRefMut<'a, V>
where V: ScryptoEncode,

Source§

impl<'c, 'a> Deref for StepCursor<'c, 'a>

Source§

impl<'k> Deref for KeyMut<'k>

Source§

impl<'rwlock, T> Deref for spin::rwlock::RwLockReadGuard<'rwlock, T>
where T: ?Sized,

Source§

impl<'rwlock, T, R> Deref for RwLockUpgradableGuard<'rwlock, T, R>
where T: ?Sized,

Source§

impl<'rwlock, T, R> Deref for spin::rwlock::RwLockWriteGuard<'rwlock, T, R>
where T: ?Sized,

Source§

impl<'str> Deref for EitherOsStr<'str>

Source§

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

Source§

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

1.0.0 · Source§

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

Source§

impl<C> Deref for Globalizing<C>
where C: HasStub,

Source§

type Target = <C as HasStub>::Stub

Source§

impl<C> Deref for scrypto::component::component::Owned<C>
where C: HasStub,

Source§

type Target = <C as HasStub>::Stub

Source§

impl<Data> Deref for NonFungibleResourceManagerDataEntryPayload<Data>

Source§

type Target = Data

Source§

impl<H, T> Deref for ThinArc<H, T>

Source§

impl<I> Deref for Located<I>

Source§

impl<I> Deref for Partial<I>

Source§

impl<I, S> Deref for Stateful<I, S>

Source§

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

Source§

type Target = <L as Deref>::Target

Source§

impl<O> Deref for Global<O>
where O: HasStub,

Source§

type Target = <O as HasStub>::Stub

1.33.0 · Source§

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

Source§

type Target = <Ptr as Deref>::Target

Source§

impl<Results> Deref for TypedResumableInvocation<Results>

1.0.0 (const: unstable) · Source§

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

1.0.0 (const: unstable) · Source§

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

1.20.0 · Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

Source§

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

Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::MappedMutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::MappedRwLockReadGuard<'_, T>
where T: ?Sized,

Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::MappedRwLockWriteGuard<'_, T>
where T: ?Sized,

1.0.0 · Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::MutexGuard<'_, T>
where T: ?Sized,

Source§

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

1.0.0 · Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::RwLockReadGuard<'_, T>
where T: ?Sized,

1.0.0 · Source§

impl<T> Deref for scrypto_test::prelude::rust::sync::RwLockWriteGuard<'_, T>
where T: ?Sized,

1.9.0 · Source§

impl<T> Deref for AssertUnwindSafe<T>

Source§

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

Source§

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

Source§

impl<T> Deref for AliasableVec<T>

Source§

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

Source§

impl<T> Deref for CachePadded<T>

Source§

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

Source§

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

Source§

impl<T> Deref for SharedPtr<T>
where T: SharedPtrTarget,

Source§

impl<T> Deref for UniquePtr<T>
where T: UniquePtrTarget,

Source§

impl<T> Deref for triomphe::arc::Arc<T>
where T: ?Sized,

Source§

impl<T> Deref for OffsetArc<T>

Source§

impl<T> Deref for triomphe::unique_arc::UniqueArc<T>
where T: ?Sized,

1.0.0 · Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

Source§

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

Source§

impl<T, A> Deref for scrypto_test::prelude::rust::sync::UniqueArc<T, A>
where A: Allocator, T: ?Sized,

1.12.0 · Source§

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

1.80.0 · Source§

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

1.80.0 · Source§

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

Source§

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

Source§

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

Source§

impl<T, F> Deref for regex_automata::util::lazy::Lazy<T, F>
where F: Fn() -> T,

Source§

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

Source§

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

Source§

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

Source§

impl<V> Deref for DataRef<V>
where V: ScryptoEncode,

Source§

impl<V> Deref for DataRefMut<V>
where V: ScryptoEncode,

Source§

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

Source§

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