Trait sp_std::ops::Deref

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

1.0.0 · source§

impl Deref for CString

§

type Target = CStr

1.0.0 · source§

impl Deref for String

§

type Target = str

1.0.0 · source§

impl Deref for OsString

1.0.0 · source§

impl Deref for PathBuf

§

type Target = Path

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, 'f> Deref for VaList<'a, 'f>
where 'f: 'a,

§

type Target = VaListImpl<'f>

1.0.0 · source§

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

§

type Target = B

1.33.0 · source§

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

§

type Target = <Ptr as Deref>::Target

1.0.0 · source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

1.0.0 · 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

source§

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

§

type Target = T

source§

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

§

type Target = T

source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

1.9.0 · source§

impl<T> Deref for AssertUnwindSafe<T>

§

type Target = T

1.0.0 · source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = T

1.0.0 · source§

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

§

type Target = [T]

1.12.0 · source§

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

§

type Target = T

1.80.0 · source§

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

§

type Target = T

1.80.0 · source§

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

§

type Target = T

impl<K, V, S> Deref for AHashMap<K, V, S>

impl<T, S> Deref for AHashSet<T, S>

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

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

impl Deref for Error

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

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

impl<T> Deref for SequenceOf<T>

impl<T> Deref for SetOf<T>

impl<T, D> Deref for FramedRead<T, D>

impl<T, E> Deref for FramedWrite<T, E>

impl<T, U> Deref for Framed<T, U>

impl<A, O> Deref for BitArray<A, O>
where A: BitViewSized, O: BitOrder,

impl<M, T, O> Deref for BitRef<'_, M, T, O>
where M: Mutability, T: BitStore, O: BitOrder,

impl<T, O> Deref for BitBox<T, O>
where T: BitStore, O: BitOrder,

impl<T, O> Deref for BitVec<T, O>
where T: BitStore, O: BitOrder,

impl<'a, T, S> Deref for BoundedSlice<'a, T, S>

impl<K, V, S> Deref for BoundedBTreeMap<K, V, S>
where K: Ord,

impl<T, S> Deref for BoundedBTreeSet<T, S>
where T: Ord,

impl<T, S> Deref for BoundedVec<T, S>

impl<T, S> Deref for WeakBoundedVec<T, S>

impl Deref for Bytes

impl Deref for BytesMut

impl Deref for Families

impl Deref for HasAtomics

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<T> Deref for NonZero<T>
where T: Zero,

impl<'a, K: Eq + Hash, S: BuildHasher> Deref for RefMulti<'a, K, S>

impl<'a, K: Eq + Hash, S: BuildHasher> Deref for Ref<'a, K, S>

impl<'a, K: Eq + Hash, V, S: BuildHasher> Deref for RefMulti<'a, K, V, S>

impl<'a, K: Eq + Hash, V, S: BuildHasher> Deref for RefMutMulti<'a, K, V, S>

impl<'a, K: Eq + Hash, V, S: BuildHasher> Deref for Ref<'a, K, V, S>

impl<'a, K: Eq + Hash, V, S: BuildHasher> Deref for RefMut<'a, K, V, S>

impl<'a, K: Eq + Hash, V, T, S: BuildHasher> Deref for MappedRef<'a, K, V, T, S>

impl<'a, K: Eq + Hash, V, T, S: BuildHasher> Deref for MappedRefMut<'a, K, V, T, S>

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<C> Deref for NonZeroScalar<C>
where C: CurveArithmetic,

impl<P> Deref for NonIdentity<P>

impl<T> Deref for FileGuard<T>
where T: Deref<Target = File>,

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

impl Deref for WakerRef<'_>

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

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

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

impl Deref for Message

impl Deref for ZoneUsage

impl Deref for A

impl Deref for AAAA

impl Deref for HTTPS

impl Deref for ANAME

impl Deref for CNAME

impl Deref for NS

impl Deref for PTR

impl Deref for DnsRequest

impl Deref for Duration

impl Deref for Timestamp

impl Deref for Collator

impl Deref for Exception

impl Deref for Global

impl Deref for Instance

impl Deref for LinkError

impl Deref for Memory

impl Deref for Module

impl Deref for Table

impl Deref for Tag

impl Deref for Array

impl Deref for BigInt

impl Deref for Boolean

impl Deref for DataView

impl Deref for Date

impl Deref for Error

impl Deref for EvalError

impl Deref for Function

impl Deref for Generator

impl Deref for Int16Array

impl Deref for Int32Array

impl Deref for Int8Array

impl Deref for Iterator

impl Deref for JsString

impl Deref for Map

impl Deref for Number

impl Deref for Object

impl Deref for Promise

impl Deref for Proxy

impl Deref for RangeError

impl Deref for RegExp

impl Deref for Set

impl Deref for Symbol

impl Deref for TypeError

impl Deref for Uint8Array

impl Deref for UriError

impl Deref for WeakMap

impl Deref for WeakSet

impl<Context> Deref for RpcModule<Context>

impl Deref for Connection

impl Deref for Transport

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 Deref for Mmap

impl Deref for MmapMut

impl Deref for Buffer

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

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

impl<T: Float> Deref for NotNan<T>

impl<T: Float> Deref for OrderedFloat<T>

impl<'a, T: EncodeLike<U>, U: Encode> Deref for Ref<'a, T, U>

impl<'a> Deref for Ident<'a>

impl<'a, G> Deref for Frozen<'a, G>

impl<'b, T> Deref for Ptr<'b, T>

impl Deref for OurView

impl<'a> Deref for ProgramSymbol<'a>

impl<'a> Deref for CowBytes<'a>

impl<T> Deref for CacheAligned<T>

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

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

impl<T> Deref for MaybeTimeOfFlight<T>

impl<T> Deref for MeteredReceiver<T>

impl<T> Deref for MeteredSender<T>

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

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

impl<'a> Deref for DBPinnableSlice<'a>

impl Deref for SafeString

impl Deref for SafeVec

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<'a, T> Deref for SharedDataLocked<'a, T>

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

impl<T> Deref for SendWrapper<T>

impl Deref for ByteBuf

impl Deref for Bytes

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

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<'a> Deref for CNAME<'a>

impl<'a> Deref for HTTPS<'a>

impl<'a> Deref for MB<'a>

impl<'a> Deref for MD<'a>

impl<'a> Deref for MF<'a>

impl<'a> Deref for MG<'a>

impl<'a> Deref for MR<'a>

impl<'a> Deref for NS<'a>

impl<'a> Deref for NSAP_PTR<'a>

impl<'a> Deref for PTR<'a>

impl<'a> Deref for X25<'a>

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

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

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

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

impl Deref for Signature

impl Deref for Signature

impl Deref for Signature

impl Deref for Slot

impl Deref for Bytes

impl<const N: usize, T> Deref for CryptoBytes<N, T>

impl<T, D: Get<T>> Deref for TypeWithDefault<T, D>

impl<Xt> Deref for ExtrinsicWrapper<Xt>

impl Deref for Timestamp

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

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

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

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for ReadGuard<'a, T>
where T: 'static,

impl<'a, T> Deref for WriteGuard<'a, T>
where T: 'static,

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

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

impl<T> Deref for ConstStatic<T>

impl<T> Deref for Static<T>

impl<T, G> Deref for LazyFinalize<T, G>
where G: 'static + Generator<T> + Sync, T: 'static + Sync + Finaly,

impl<T, G> Deref for LesserLazy<T, G>
where G: 'static + Generator<T>, T: 'static,

impl<T, G> Deref for LesserLazyFinalize<T, G>
where G: 'static + Generator<T> + Sync, T: 'static + Sync + Finaly,

impl<T, G> Deref for Lazy<T, G>
where G: Generator<T>,

impl<T, G> Deref for UnSyncLazy<T, G>
where G: Generator<T>,

impl Deref for TempPath

impl<C> Deref for ReadHalf<C>
where C: Read,

impl<C> Deref for WriteHalf<C>
where C: Write,

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<E: ?Sized> Deref for FormattedFields<E>

impl Deref for Bytes

impl Deref for Message

impl Deref for ZoneUsage

impl Deref for A

impl Deref for AAAA

impl Deref for HTTPS

impl Deref for ANAME

impl Deref for CNAME

impl Deref for NS

impl Deref for PTR

impl Deref for DnsRequest

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

impl<T> Deref for Clamped<T>

impl<T: FromWasmAbi + 'static> Deref for JsStatic<T>

impl Deref for KebabStr

impl Deref for MmapVec

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

impl<T> Deref for FmtList<T>
where for<'a> &'a T: IntoIterator,

impl<T: Binary> Deref for FmtBinary<T>

impl<T: Display> Deref for FmtDisplay<T>

impl<T: LowerExp> Deref for FmtLowerExp<T>

impl<T: LowerHex> Deref for FmtLowerHex<T>

impl<T: Octal> Deref for FmtOctal<T>

impl<T: Pointer> Deref for FmtPointer<T>

impl<T: UpperExp> Deref for FmtUpperExp<T>

impl<T: UpperHex> Deref for FmtUpperHex<T>

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

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

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

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

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

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