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