]> git.lizzy.rs Git - rust.git/blob - library/core/src/ops/deref.rs
Rollup merge of #75780 - matklad:unconfuseunpindocs, r=KodrAus
[rust.git] / library / core / src / ops / deref.rs
1 /// Used for immutable dereferencing operations, like `*v`.
2 ///
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.
7 ///
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.
13 ///
14 /// For similar reasons, **this trait should never fail**. Failure during
15 /// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
16 ///
17 /// # More on `Deref` coercion
18 ///
19 /// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
20 ///
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`.
25 ///
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].
29 ///
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
35 ///
36 /// # Examples
37 ///
38 /// A struct with a single field which is accessible by dereferencing the
39 /// struct.
40 ///
41 /// ```
42 /// use std::ops::Deref;
43 ///
44 /// struct DerefExample<T> {
45 ///     value: T
46 /// }
47 ///
48 /// impl<T> Deref for DerefExample<T> {
49 ///     type Target = T;
50 ///
51 ///     fn deref(&self) -> &Self::Target {
52 ///         &self.value
53 ///     }
54 /// }
55 ///
56 /// let x = DerefExample { value: 'a' };
57 /// assert_eq!('a', *x);
58 /// ```
59 #[lang = "deref"]
60 #[doc(alias = "*")]
61 #[doc(alias = "&*")]
62 #[stable(feature = "rust1", since = "1.0.0")]
63 pub trait Deref {
64     /// The resulting type after dereferencing.
65     #[stable(feature = "rust1", since = "1.0.0")]
66     type Target: ?Sized;
67
68     /// Dereferences the value.
69     #[must_use]
70     #[stable(feature = "rust1", since = "1.0.0")]
71     fn deref(&self) -> &Self::Target;
72 }
73
74 #[stable(feature = "rust1", since = "1.0.0")]
75 impl<T: ?Sized> Deref for &T {
76     type Target = T;
77
78     fn deref(&self) -> &T {
79         *self
80     }
81 }
82
83 #[stable(feature = "rust1", since = "1.0.0")]
84 impl<T: ?Sized> !DerefMut for &T {}
85
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl<T: ?Sized> Deref for &mut T {
88     type Target = T;
89
90     fn deref(&self) -> &T {
91         *self
92     }
93 }
94
95 /// Used for mutable dereferencing operations, like in `*v = 1;`.
96 ///
97 /// In addition to being used for explicit dereferencing operations with the
98 /// (unary) `*` operator in mutable contexts, `DerefMut` is also used implicitly
99 /// by the compiler in many circumstances. This mechanism is called
100 /// ['`Deref` coercion'][more]. In immutable contexts, [`Deref`] is used.
101 ///
102 /// Implementing `DerefMut` for smart pointers makes mutating the data behind
103 /// them convenient, which is why they implement `DerefMut`. On the other hand,
104 /// the rules regarding [`Deref`] and `DerefMut` were designed specifically to
105 /// accommodate smart pointers. Because of this, **`DerefMut` should only be
106 /// implemented for smart pointers** to avoid confusion.
107 ///
108 /// For similar reasons, **this trait should never fail**. Failure during
109 /// dereferencing can be extremely confusing when `DerefMut` is invoked
110 /// implicitly.
111 ///
112 /// # More on `Deref` coercion
113 ///
114 /// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
115 /// then:
116 ///
117 /// * In mutable contexts, `*x` (where `T` is neither a reference nor a raw pointer)
118 ///   is equivalent to `*DerefMut::deref_mut(&mut x)`.
119 /// * Values of type `&mut T` are coerced to values of type `&mut U`
120 /// * `T` implicitly implements all the (mutable) methods of the type `U`.
121 ///
122 /// For more details, visit [the chapter in *The Rust Programming Language*][book]
123 /// as well as the reference sections on [the dereference operator][ref-deref-op],
124 /// [method resolution] and [type coercions].
125 ///
126 /// [book]: ../../book/ch15-02-deref.html
127 /// [more]: #more-on-deref-coercion
128 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
129 /// [method resolution]: ../../reference/expressions/method-call-expr.html
130 /// [type coercions]: ../../reference/type-coercions.html
131 ///
132 /// # Examples
133 ///
134 /// A struct with a single field which is modifiable by dereferencing the
135 /// struct.
136 ///
137 /// ```
138 /// use std::ops::{Deref, DerefMut};
139 ///
140 /// struct DerefMutExample<T> {
141 ///     value: T
142 /// }
143 ///
144 /// impl<T> Deref for DerefMutExample<T> {
145 ///     type Target = T;
146 ///
147 ///     fn deref(&self) -> &Self::Target {
148 ///         &self.value
149 ///     }
150 /// }
151 ///
152 /// impl<T> DerefMut for DerefMutExample<T> {
153 ///     fn deref_mut(&mut self) -> &mut Self::Target {
154 ///         &mut self.value
155 ///     }
156 /// }
157 ///
158 /// let mut x = DerefMutExample { value: 'a' };
159 /// *x = 'b';
160 /// assert_eq!('b', *x);
161 /// ```
162 #[lang = "deref_mut"]
163 #[doc(alias = "*")]
164 #[stable(feature = "rust1", since = "1.0.0")]
165 pub trait DerefMut: Deref {
166     /// Mutably dereferences the value.
167     #[stable(feature = "rust1", since = "1.0.0")]
168     fn deref_mut(&mut self) -> &mut Self::Target;
169 }
170
171 #[stable(feature = "rust1", since = "1.0.0")]
172 impl<T: ?Sized> DerefMut for &mut T {
173     fn deref_mut(&mut self) -> &mut T {
174         *self
175     }
176 }
177
178 /// Indicates that a struct can be used as a method receiver, without the
179 /// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
180 /// `Rc<T>`, `&T`, and `Pin<P>`.
181 #[lang = "receiver"]
182 #[unstable(feature = "receiver_trait", issue = "none")]
183 #[doc(hidden)]
184 pub trait Receiver {
185     // Empty.
186 }
187
188 #[unstable(feature = "receiver_trait", issue = "none")]
189 impl<T: ?Sized> Receiver for &T {}
190
191 #[unstable(feature = "receiver_trait", issue = "none")]
192 impl<T: ?Sized> Receiver for &mut T {}