]>
git.proxmox.com Git - rustc.git/blob - library/core/src/ops/deref.rs
1 /// Used for immutable dereferencing operations, like `*v`.
3 /// In addition to being used for explicit dereferencing operations with the
4 /// (unary) `*` operator in immutable contexts, `Deref` is also used implicitly
5 /// by the compiler in many circumstances. This mechanism is called
6 /// ['`Deref` coercion'][more]. In mutable contexts, [`DerefMut`] is used.
8 /// Implementing `Deref` for smart pointers makes accessing the data behind them
9 /// convenient, which is why they implement `Deref`. On the other hand, the
10 /// rules regarding `Deref` and [`DerefMut`] were designed specifically to
11 /// accommodate smart pointers. Because of this, **`Deref` should only be
12 /// implemented for smart pointers** to avoid confusion.
14 /// For similar reasons, **this trait should never fail**. Failure during
15 /// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
17 /// # More on `Deref` coercion
19 /// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
21 /// * In immutable contexts, `*x` (where `T` is neither a reference nor a raw pointer)
22 /// is equivalent to `*Deref::deref(&x)`.
23 /// * Values of type `&T` are coerced to values of type `&U`
24 /// * `T` implicitly implements all the (immutable) methods of the type `U`.
26 /// For more details, visit [the chapter in *The Rust Programming Language*][book]
27 /// as well as the reference sections on [the dereference operator][ref-deref-op],
28 /// [method resolution] and [type coercions].
30 /// [book]: ../../book/ch15-02-deref.html
31 /// [more]: #more-on-deref-coercion
32 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
33 /// [method resolution]: ../../reference/expressions/method-call-expr.html
34 /// [type coercions]: ../../reference/type-coercions.html
38 /// A struct with a single field which is accessible by dereferencing the
42 /// use std::ops::Deref;
44 /// struct DerefExample<T> {
48 /// impl<T> Deref for DerefExample<T> {
51 /// fn deref(&self) -> &Self::Target {
56 /// let x = DerefExample { value: 'a' };
57 /// assert_eq!('a', *x);
62 #[stable(feature = "rust1", since = "1.0.0")]
63 #[rustc_diagnostic_item = "Deref"]
66 /// The resulting type after dereferencing.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 #[rustc_diagnostic_item = "deref_target"]
69 #[lang = "deref_target"]
72 /// Dereferences the value.
74 #[stable(feature = "rust1", since = "1.0.0")]
75 #[rustc_diagnostic_item = "deref_method"]
76 fn deref(&self) -> &Self::Target
;
79 #[stable(feature = "rust1", since = "1.0.0")]
80 #[rustc_const_unstable(feature = "const_deref", issue = "88955")]
81 impl<T
: ?Sized
> const Deref
for &T
{
84 #[rustc_diagnostic_item = "noop_method_deref"]
85 fn deref(&self) -> &T
{
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl<T
: ?Sized
> !DerefMut
for &T {}
93 #[stable(feature = "rust1", since = "1.0.0")]
94 #[rustc_const_unstable(feature = "const_deref", issue = "88955")]
95 impl<T
: ?Sized
> const Deref
for &mut T
{
98 fn deref(&self) -> &T
{
103 /// Used for mutable dereferencing operations, like in `*v = 1;`.
105 /// In addition to being used for explicit dereferencing operations with the
106 /// (unary) `*` operator in mutable contexts, `DerefMut` is also used implicitly
107 /// by the compiler in many circumstances. This mechanism is called
108 /// ['`Deref` coercion'][more]. In immutable contexts, [`Deref`] is used.
110 /// Implementing `DerefMut` for smart pointers makes mutating the data behind
111 /// them convenient, which is why they implement `DerefMut`. On the other hand,
112 /// the rules regarding [`Deref`] and `DerefMut` were designed specifically to
113 /// accommodate smart pointers. Because of this, **`DerefMut` should only be
114 /// implemented for smart pointers** to avoid confusion.
116 /// For similar reasons, **this trait should never fail**. Failure during
117 /// dereferencing can be extremely confusing when `DerefMut` is invoked
120 /// # More on `Deref` coercion
122 /// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
125 /// * In mutable contexts, `*x` (where `T` is neither a reference nor a raw pointer)
126 /// is equivalent to `*DerefMut::deref_mut(&mut x)`.
127 /// * Values of type `&mut T` are coerced to values of type `&mut U`
128 /// * `T` implicitly implements all the (mutable) methods of the type `U`.
130 /// For more details, visit [the chapter in *The Rust Programming Language*][book]
131 /// as well as the reference sections on [the dereference operator][ref-deref-op],
132 /// [method resolution] and [type coercions].
134 /// [book]: ../../book/ch15-02-deref.html
135 /// [more]: #more-on-deref-coercion
136 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
137 /// [method resolution]: ../../reference/expressions/method-call-expr.html
138 /// [type coercions]: ../../reference/type-coercions.html
142 /// A struct with a single field which is modifiable by dereferencing the
146 /// use std::ops::{Deref, DerefMut};
148 /// struct DerefMutExample<T> {
152 /// impl<T> Deref for DerefMutExample<T> {
155 /// fn deref(&self) -> &Self::Target {
160 /// impl<T> DerefMut for DerefMutExample<T> {
161 /// fn deref_mut(&mut self) -> &mut Self::Target {
166 /// let mut x = DerefMutExample { value: 'a' };
168 /// assert_eq!('b', x.value);
170 #[lang = "deref_mut"]
172 #[stable(feature = "rust1", since = "1.0.0")]
174 pub trait DerefMut
: Deref
{
175 /// Mutably dereferences the value.
176 #[stable(feature = "rust1", since = "1.0.0")]
177 fn deref_mut(&mut self) -> &mut Self::Target
;
180 #[stable(feature = "rust1", since = "1.0.0")]
181 impl<T
: ?Sized
> DerefMut
for &mut T
{
182 fn deref_mut(&mut self) -> &mut T
{
187 /// Indicates that a struct can be used as a method receiver, without the
188 /// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
189 /// `Rc<T>`, `&T`, and `Pin<P>`.
191 #[unstable(feature = "receiver_trait", issue = "none")]
197 #[unstable(feature = "receiver_trait", issue = "none")]
198 impl<T
: ?Sized
> Receiver
for &T {}
200 #[unstable(feature = "receiver_trait", issue = "none")]
201 impl<T
: ?Sized
> Receiver
for &mut T {}