wasmtime_environ::__core::ops

Trait Deref

1.6.0 · Source
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 (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

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

Dereferences the value.

Implementors§

Source§

impl Deref for Error

Source§

type Target = dyn Error + Send + Sync

Source§

impl Deref for BuildMetadata

Source§

impl Deref for Prerelease

Source§

impl Deref for KebabStr

Source§

impl Deref for KebabString

1.0.0 · Source§

impl Deref for String

1.0.0 · Source§

impl Deref for CString

1.0.0 · Source§

impl Deref for OsString

1.0.0 · Source§

impl Deref for PathBuf

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, R> Deref for UnitRef<'a, R>
where R: Reader,

Source§

impl<'input, Endian> Deref for EndianSlice<'input, Endian>
where Endian: Endianity,

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

1.33.0 · Source§

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

Source§

type Target = <Ptr as Deref>::Target

1.0.0 · Source§

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

1.0.0 · Source§

impl<T> Deref for &mut 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,

1.20.0 · Source§

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

1.9.0 · Source§

impl<T> Deref for AssertUnwindSafe<T>

Source§

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

Source§

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

1.0.0 · Source§

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

Source§

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

Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

Source§

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

Source§

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

Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

1.12.0 · Source§

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

1.0.0 · Source§

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

Source§

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

1.0.0 · Source§

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

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<W> Deref for DebugAbbrev<W>
where W: Writer,

Source§

impl<W> Deref for DebugFrame<W>
where W: Writer,

Source§

impl<W> Deref for EhFrame<W>
where W: Writer,

Source§

impl<W> Deref for DebugLine<W>
where W: Writer,

Source§

impl<W> Deref for DebugLoc<W>
where W: Writer,

Source§

impl<W> Deref for DebugLocLists<W>
where W: Writer,

Source§

impl<W> Deref for DebugRanges<W>
where W: Writer,

Source§

impl<W> Deref for DebugRngLists<W>
where W: Writer,

Source§

impl<W> Deref for DebugLineStr<W>
where W: Writer,

Source§

impl<W> Deref for DebugStr<W>
where W: Writer,

Source§

impl<W> Deref for DebugInfo<W>
where W: Writer,

impl<T: RefCnt, S: Strategy<T>> Deref for Guard<T, S>

impl<T> Deref for SequenceOf<T>

impl<T> Deref for SetOf<T>

impl<T> Deref for RwLockReadGuardArc<T>

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

impl<T: ?Sized> Deref for MutexGuardArc<T>

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

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

impl Deref for Message

impl Deref for Subject

impl<S> Deref for State<S>

impl<T> Deref for ConnectInfo<T>

impl<T> Deref for Path<T>

impl<T> Deref for Extension<T>

impl<E, R> Deref for WithRejection<E, R>

impl<T> Deref for Cached<T>

impl Deref for Bytes

impl Deref for BytesMut

impl<S: Storage> Deref for StrInner<S>

impl Deref for OsStr

impl Deref for Str

impl Deref for Function

impl Deref for Gpr

impl Deref for Xmm

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

impl<T> Deref for CachePadded<T>

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

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

impl<M: Manager> Deref for Object<M>

impl<'a> Deref for Transaction<'a>

impl<'a> Deref for TransactionBuilder<'a>

impl Deref for Ia5String

impl<'a> Deref for Ia5StringRef<'a>

impl<'a> Deref for PrintableStringRef<'a>

impl<'a> Deref for TeletexStringRef<'a>

impl<'a> Deref for Utf8StringRef<'a>

impl<'a> Deref for VideotexStringRef<'a>

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

impl<S: Stream + Unpin> Deref for BlockingStream<S>

impl Deref for WakerRef<'_>

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

impl<T: ?Sized> Deref for OwnedMutexGuard<T>

impl<T: ?Sized, U: ?Sized> Deref for MappedMutexGuard<'_, T, U>

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

impl Deref for Trailers

impl Deref for Duration

impl Deref for Timestamp

impl Deref for Private

impl Deref for Attributes

impl Deref for Variants

impl Deref for Cart

impl<Target: FilelikeViewType> Deref for FilelikeView<'_, Target>

impl<Target: SocketlikeViewType> Deref for SocketlikeView<'_, Target>

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

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

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

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

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

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

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

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

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

impl<T> Deref for MaybeOwned<'_, T>

impl<T> Deref for MaybeOwnedMut<'_, T>

impl Deref for NUIDStr

impl Deref for WasmClient

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

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

impl Deref for SpanEvents

impl Deref for SpanLinks

impl<T> Deref for Metadata<'_, T>
where T: SmartDisplay + ?Sized,

impl<const SIZE: usize> Deref for WriteBuffer<SIZE>

impl<'a> Deref for Event<'a>

impl<'a> Deref for Text<'a>

impl<'a> Deref for BytesCData<'a>

impl<'a> Deref for BytesDecl<'a>

impl<'a> Deref for BytesEnd<'a>

impl<'a> Deref for BytesStart<'a>

impl<'a> Deref for BytesText<'a>

impl<R> Deref for NsReader<R>

impl Deref for InfoDict

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

impl<T, F: Fn() -> T> Deref for Lazy<T, F>

impl Deref for Connection

impl Deref for Connection

impl Deref for BorrowedPayload<'_>

impl<Data> Deref for ConnectionCommon<Data>

impl<T> Deref for ConnectionCommon<T>

impl Deref for CertificateDer<'_>

impl Deref for Der<'_>

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

impl<'a, T, C> Deref for Ref<'a, T, C>
where T: Clear + Default, C: Config,

impl<'a, T, C> Deref for RefMut<'a, T, C>
where T: Clear + Default, C: Config,

impl<'a, T, C: Config> Deref for Entry<'a, T, C>

impl<T, C> Deref for OwnedRef<T, C>
where T: Clear + Default, C: Config,

impl<T, C> Deref for OwnedRefMut<T, C>
where T: Clear + Default, C: Config,

impl<T, C> Deref for OwnedEntry<T, C>
where C: Config,

impl Deref for KeyName

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

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

impl Deref for Gid

impl Deref for Uid

impl<const N: usize> Deref for TinyAsciiStr<N>

impl<'s, T> Deref for SliceVec<'s, T>

impl<A: Array> Deref for TinyVec<A>

impl<A: Array> Deref for ArrayVec<A>

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

impl<T> Deref for Ref<'_, T>

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

impl<T: ?Sized> Deref for OwnedMutexGuard<T>

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

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

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

impl<T: ?Sized, U: ?Sized> Deref for OwnedMappedMutexGuard<T, U>

impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockMappedWriteGuard<T, U>

impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockReadGuard<T, U>

impl Deref for Key

impl Deref for KeyMut<'_>

impl<S> Deref for ImDocument<S>

impl<E: ?Sized> Deref for FormattedFields<E>

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

impl<S> Deref for Ascii<S>

impl<S> Deref for UniCase<S>

impl Deref for Extensions

impl<T: GcRef> Deref for ManuallyRooted<T>

impl<T: GcRef> Deref for Rooted<T>

impl<'a> Deref for EndEntityCert<'a>

impl Deref for BStr

impl Deref for Bytes

impl<I> Deref for LocatingSlice<I>

impl<I> Deref for Partial<I>

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

impl<T> Deref for TokenSlice<'_, T>

impl Deref for Source

impl<T> Deref for SyncCodec<T>

impl<T, F> Deref for AcceptMapContext<T, F>

impl Deref for Subscriber

impl<'a> Deref for X509Certificate<'a>

impl<'a> Deref for CRLDistributionPoints<'a>

impl<'a> Deref for StrSpan<'a>

impl<C0, C1, T> Deref for EitherCart<C0, C1>
where C0: Deref<Target = T>, C1: Deref<Target = T>,

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

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

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

impl<'a> Deref for FlexZeroVec<'a>

impl<'a, T: AsULE> Deref for ZeroVec<'a, T>

impl<T: VarULE + ?Sized, F: VarZeroVecFormat> Deref for VarZeroVec<'_, T, F>