devela::all

Trait Deref

1.0.0 · Source
pub trait Deref {
    type Target: ?Sized;

    // Required method
    fn deref(&self) -> &Self::Target;
}
Expand description

core Used for immutable dereferencing operations, like *v.

Re-exported from core::ops:: .


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§

§

impl Deref for BoxBytes

§

type Target = [u8]

Source§

impl Deref for Collator

Source§

impl Deref for DateTimeFormat

Source§

impl Deref for NumberFormat

Source§

impl Deref for PluralRules

Source§

impl Deref for RelativeTimeFormat

Source§

impl Deref for CompileError

Source§

impl Deref for Exception

Source§

impl Deref for Global

Source§

impl Deref for Instance

Source§

impl Deref for LinkError

Source§

impl Deref for Memory

Source§

impl Deref for Module

Source§

impl Deref for RuntimeError

Source§

impl Deref for Table

Source§

impl Deref for Tag

Source§

impl Deref for devela::_dep::js_sys::Array

Source§

impl Deref for ArrayBuffer

Source§

impl Deref for AsyncIterator

Source§

impl Deref for BigInt64Array

Source§

impl Deref for BigInt

Source§

impl Deref for BigUint64Array

Source§

impl Deref for Boolean

Source§

impl Deref for DataView

Source§

impl Deref for Date

Source§

impl Deref for Error

Source§

impl Deref for EvalError

Source§

impl Deref for Float32Array

Source§

impl Deref for Float64Array

Source§

impl Deref for Function

Source§

impl Deref for Generator

Source§

impl Deref for Int8Array

Source§

impl Deref for Int16Array

Source§

impl Deref for Int32Array

Source§

impl Deref for Iterator

Source§

impl Deref for IteratorNext

Source§

impl Deref for JsString

Source§

impl Deref for Map

Source§

impl Deref for Number

Source§

impl Deref for Object

Source§

impl Deref for Promise

Source§

impl Deref for Proxy

Source§

impl Deref for RangeError

Source§

impl Deref for ReferenceError

Source§

impl Deref for RegExp

Source§

impl Deref for Set

Source§

impl Deref for SharedArrayBuffer

Source§

impl Deref for Symbol

Source§

impl Deref for SyntaxError

Source§

impl Deref for TypeError

Source§

impl Deref for Uint8Array

Source§

impl Deref for Uint8ClampedArray

Source§

impl Deref for Uint16Array

Source§

impl Deref for Uint32Array

Source§

impl Deref for UriError

Source§

impl Deref for WeakMap

Source§

impl Deref for WeakSet

§

impl Deref for devela::_dep::nc::c_str::CString

§

type Target = CStr

§

impl Deref for CancelledError

§

impl Deref for IncompleteReadError

§

impl Deref for InvalidStateError

§

impl Deref for LimitOverrunError

§

impl Deref for QueueEmpty

§

impl Deref for QueueFull

§

impl Deref for TimeoutError

§

impl Deref for gaierror

§

impl Deref for herror

§

impl Deref for timeout

§

impl Deref for PyArithmeticError

§

impl Deref for PyAssertionError

§

impl Deref for PyAttributeError

§

impl Deref for PyBaseException

§

impl Deref for PyBlockingIOError

§

impl Deref for PyBrokenPipeError

§

impl Deref for PyBufferError

§

impl Deref for PyBytesWarning

§

impl Deref for PyChildProcessError

§

impl Deref for PyConnectionAbortedError

§

impl Deref for PyConnectionError

§

impl Deref for PyConnectionRefusedError

§

impl Deref for PyConnectionResetError

§

impl Deref for PyDeprecationWarning

§

impl Deref for PyEOFError

§

impl Deref for PyEncodingWarning

§

impl Deref for PyEnvironmentError

§

impl Deref for PyException

§

impl Deref for PyFileExistsError

§

impl Deref for PyFileNotFoundError

§

impl Deref for PyFloatingPointError

§

impl Deref for PyFutureWarning

§

impl Deref for PyGeneratorExit

§

impl Deref for PyIOError

§

impl Deref for PyImportError

§

impl Deref for PyImportWarning

§

impl Deref for PyIndexError

§

impl Deref for PyInterruptedError

§

impl Deref for PyIsADirectoryError

§

impl Deref for PyKeyError

§

impl Deref for PyKeyboardInterrupt

§

impl Deref for PyLookupError

§

impl Deref for PyMemoryError

§

impl Deref for PyModuleNotFoundError

§

impl Deref for PyNameError

§

impl Deref for PyNotADirectoryError

§

impl Deref for PyNotImplementedError

§

impl Deref for PyOSError

§

impl Deref for PyOverflowError

§

impl Deref for PyPendingDeprecationWarning

§

impl Deref for PyPermissionError

§

impl Deref for PyProcessLookupError

§

impl Deref for PyRecursionError

§

impl Deref for PyReferenceError

§

impl Deref for PyResourceWarning

§

impl Deref for PyRuntimeError

§

impl Deref for PyRuntimeWarning

§

impl Deref for PyStopAsyncIteration

§

impl Deref for PyStopIteration

§

impl Deref for PySyntaxError

§

impl Deref for PySyntaxWarning

§

impl Deref for PySystemError

§

impl Deref for PySystemExit

§

impl Deref for PyTimeoutError

§

impl Deref for PyTypeError

§

impl Deref for PyUnboundLocalError

§

impl Deref for PyUnicodeDecodeError

§

impl Deref for PyUnicodeEncodeError

§

impl Deref for PyUnicodeError

§

impl Deref for PyUnicodeTranslateError

§

impl Deref for PyUnicodeWarning

§

impl Deref for PyUserWarning

§

impl Deref for PyValueError

§

impl Deref for PyWarning

§

impl Deref for PyZeroDivisionError

§

impl Deref for PanicException

§

impl Deref for PyBackedBytes

§

type Target = [u8]

§

impl Deref for PyBackedStr

§

type Target = str

§

impl Deref for PyBool

§

impl Deref for PyByteArray

§

impl Deref for PyBytes

§

impl Deref for PyCFunction

§

impl Deref for PyCapsule

§

impl Deref for PyCode

§

impl Deref for PyComplex

§

impl Deref for PyDate

§

impl Deref for PyDateTime

§

impl Deref for PyDelta

§

impl Deref for PyDict

§

impl Deref for PyDictItems

§

impl Deref for PyDictKeys

§

impl Deref for PyDictValues

§

impl Deref for PyEllipsis

§

impl Deref for PyFloat

§

impl Deref for PyFrame

§

impl Deref for PyFrozenSet

§

impl Deref for PyFunction

§

impl Deref for PyInt

§

impl Deref for PyIterator

§

impl Deref for PyList

§

impl Deref for PyMapping

§

impl Deref for PyMappingProxy

§

impl Deref for PyMemoryView

§

impl Deref for PyModule

§

impl Deref for PyNone

§

impl Deref for PyNotImplemented

§

impl Deref for PySequence

§

impl Deref for PySet

§

impl Deref for PySlice

§

impl Deref for PyString

§

impl Deref for PySuper

§

impl Deref for PyTime

§

impl Deref for PyTraceback

§

impl Deref for PyTuple

§

impl Deref for PyType

§

impl Deref for PyTzInfo

§

impl Deref for PyWeakref

§

impl Deref for PyWeakrefProxy

§

impl Deref for PyWeakrefReference

§

impl Deref for ArchivedCString

§

type Target = CStr

§

impl Deref for Buffer<'_>

§

type Target = [u8]

§

impl Deref for ArchivedString

§

type Target = str

§

impl Deref for Components

§

impl Deref for Disks

§

type Target = [Disk]

§

impl Deref for Gid

§

type Target = u32

§

impl Deref for Groups

§

type Target = [Group]

§

impl Deref for Networks

§

impl Deref for Uid

§

type Target = u32

§

impl Deref for Users

§

type Target = [User]

§

impl Deref for EnteredSpan

§

type Target = Span

§

impl Deref for BStr

§

type Target = [u8]

§

impl Deref for devela::_dep::winnow::Bytes

§

type Target = [u8]

1.0.0 · Source§

impl Deref for devela::lang::CString

1.0.0 · Source§

impl Deref for OsString

Source§

impl Deref for Affine2

Source§

type Target = Cols3<Vec2>

Source§

impl Deref for Affine3A

Source§

type Target = Cols4<Vec3A>

Source§

impl Deref for Mat2

Source§

type Target = Cols2<Vec2>

Source§

impl Deref for Quat

Source§

type Target = Vec4<f32>

Source§

impl Deref for Vec3A

Source§

type Target = Vec3<f32>

Source§

impl Deref for Vec4

Source§

type Target = Vec4<f32>

Source§

impl Deref for DAffine2

Source§

type Target = Cols3<DVec2>

Source§

impl Deref for DAffine3

Source§

type Target = Cols4<DVec3>

1.0.0 · Source§

impl Deref for PathBuf

1.0.0 · Source§

impl Deref for devela::all::String

§

impl Deref for Bytes

§

type Target = [u8]

§

impl Deref for BytesMut

§

type Target = [u8]

§

impl Deref for MilliBel

§

type Target = i64

1.36.0 · Source§

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

1.36.0 · Source§

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

§

impl<'a> Deref for Selem<'a>

§

type Target = Elem<'a>

Source§

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

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

impl<'a, T> Deref for devela::_dep::bumpalo::boxed::Box<'a, T>
where T: ?Sized,

§

type Target = T

§

impl<'bump> Deref for devela::_dep::bumpalo::collections::String<'bump>

§

type Target = str

§

impl<'bump, T> Deref for devela::_dep::bumpalo::collections::Vec<'bump, T>
where T: 'bump,

§

type Target = [T]

§

impl<'py, T> Deref for Borrowed<'_, 'py, T>

§

type Target = Bound<'py, T>

§

impl<'py, T> Deref for Bound<'py, T>
where T: DerefToPyAny,

§

type Target = Bound<'py, PyAny>

§

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

§

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<DST: ?Sized, BUF: DstBuf> Deref for DstQueuePopHandle<'_, DST, BUF>

Available on crate feature unsafe_layout only.
Source§

type Target = DST

Source§

impl<DST: ?Sized, BUF: DstBuf> Deref for DstValue<DST, BUF>

Available on crate feature unsafe_layout only.
Source§

type Target = DST

§

impl<I> Deref for LocatingSlice<I>

§

type Target = I

§

impl<I> Deref for Partial<I>

§

type Target = I

§

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

§

type Target = I

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

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,

Source§

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

Source§

impl<T> Deref for devela::_dep::_std::sync::MappedMutexGuard<'_, T>
where T: ?Sized,

Source§

impl<T> Deref for devela::_dep::_std::sync::MappedRwLockReadGuard<'_, T>
where T: ?Sized,

Source§

impl<T> Deref for devela::_dep::_std::sync::MappedRwLockWriteGuard<'_, T>
where T: ?Sized,

Source§

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

§

impl<T> Deref for PyRef<'_, T>
where T: PyClass,

§

type Target = T

§

impl<T> Deref for PyRefMut<'_, T>
where T: PyClass<Frozen = False>,

§

type Target = T

§

impl<T> Deref for ArchivedBox<T>
where T: ArchivePointee + ?Sized,

§

type Target = T

§

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

§

type Target = T

§

impl<T> Deref for Align<T>

§

type Target = T

§

impl<T> Deref for SerVec<T>

§

type Target = [T]

§

impl<T> Deref for ArchivedVec<T>

§

type Target = [T]

Source§

impl<T> Deref for Clamped<T>

Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

1.0.0 · Source§

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

Source§

impl<T> Deref for BareBox<T>

Source§

impl<T> Deref for CacheAlign<T>

1.20.0 · Source§

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

1.9.0 · Source§

impl<T> Deref for AssertUnwindSafe<T>

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,

§

impl<T> Deref for CachePadded<T>

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

§

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

§

type Target = T

1.12.0 · Source§

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

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.0.0 · Source§

impl<T, A> Deref for devela::all::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 devela::all::Vec<T, A>
where A: Allocator,

§

impl<T, E> Deref for Strategy<T, E>
where T: ?Sized,

§

type Target = T

1.80.0 · Source§

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

§

impl<T, F> Deref for ArchivedRc<T, F>
where T: ArchivePointee + ?Sized,

§

type Target = T

1.80.0 · Source§

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

§

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

§

type Target = T

§

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

§

type Target = T

Source§

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

Source§

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

Source§

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

Available on crate feature unsafe_layout only.
Source§

impl<T, const CAP: usize, S: Storage> Deref for devela::all::Array<T, CAP, S>

§

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

§

type Target = [T]

§

impl<const A: usize> Deref for AlignedVec<A>

§

type Target = [u8]

Source§

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

Source§

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

Available on crate feature _string_nonul only.
Source§

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

Source§

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

Source§

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

Source§

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