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")]
64 /// The resulting type after dereferencing.
65 #[stable(feature = "rust1", since = "1.0.0")]
68 /// Dereferences the value.
70 #[stable(feature = "rust1", since = "1.0.0")]
71 fn deref(&self) -> &Self::Target;
74 #[stable(feature = "rust1", since = "1.0.0")]
75 impl<T: ?Sized> Deref for &T {
78 fn deref(&self) -> &T {
83 #[stable(feature = "rust1", since = "1.0.0")]
84 impl<T: ?Sized> !DerefMut for &T {}
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl<T: ?Sized> Deref for &mut T {
90 fn deref(&self) -> &T {
95 /// Used for mutable dereferencing operations, like in `*v = 1;`.
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.
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.
108 /// For similar reasons, **this trait should never fail**. Failure during
109 /// dereferencing can be extremely confusing when `DerefMut` is invoked
112 /// # More on `Deref` coercion
114 /// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
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`.
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].
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
134 /// A struct with a single field which is modifiable by dereferencing the
138 /// use std::ops::{Deref, DerefMut};
140 /// struct DerefMutExample<T> {
144 /// impl<T> Deref for DerefMutExample<T> {
147 /// fn deref(&self) -> &Self::Target {
152 /// impl<T> DerefMut for DerefMutExample<T> {
153 /// fn deref_mut(&mut self) -> &mut Self::Target {
158 /// let mut x = DerefMutExample { value: 'a' };
160 /// assert_eq!('b', *x);
162 #[lang = "deref_mut"]
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;
171 #[stable(feature = "rust1", since = "1.0.0")]
172 impl<T: ?Sized> DerefMut for &mut T {
173 fn deref_mut(&mut self) -> &mut T {
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>`.
182 #[unstable(feature = "receiver_trait", issue = "none")]
188 #[unstable(feature = "receiver_trait", issue = "none")]
189 impl<T: ?Sized> Receiver for &T {}
191 #[unstable(feature = "receiver_trait", issue = "none")]
192 impl<T: ?Sized> Receiver for &mut T {}