1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// Used for immutable dereferencing operations, like `*v`.
13 /// In addition to being used for explicit dereferencing operations with the
14 /// (unary) `*` operator in immutable contexts, `Deref` is also used implicitly
15 /// by the compiler in many circumstances. This mechanism is called
16 /// ['`Deref` coercion'][more]. In mutable contexts, [`DerefMut`] is used.
18 /// Implementing `Deref` for smart pointers makes accessing the data behind them
19 /// convenient, which is why they implement `Deref`. On the other hand, the
20 /// rules regarding `Deref` and [`DerefMut`] were designed specifically to
21 /// accommodate smart pointers. Because of this, **`Deref` should only be
22 /// implemented for smart pointers** to avoid confusion.
24 /// For similar reasons, **this trait should never fail**. Failure during
25 /// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
27 /// # More on `Deref` coercion
29 /// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
31 /// * In immutable contexts, `*x` on non-pointer types is equivalent to
32 /// `*Deref::deref(&x)`.
33 /// * Values of type `&T` are coerced to values of type `&U`
34 /// * `T` implicitly implements all the (immutable) methods of the type `U`.
36 /// For more details, visit [the chapter in *The Rust Programming Language*]
37 /// [book] as well as the reference sections on [the dereference operator]
38 /// [ref-deref-op], [method resolution] and [type coercions].
40 /// [book]: ../../book/second-edition/ch15-02-deref.html
41 /// [`DerefMut`]: trait.DerefMut.html
42 /// [more]: #more-on-deref-coercion
43 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
44 /// [method resolution]: ../../reference/expressions/method-call-expr.html
45 /// [type coercions]: ../../reference/type-coercions.html
49 /// A struct with a single field which is accessible by dereferencing the
53 /// use std::ops::Deref;
55 /// struct DerefExample<T> {
59 /// impl<T> Deref for DerefExample<T> {
62 /// fn deref(&self) -> &T {
67 /// let x = DerefExample { value: 'a' };
68 /// assert_eq!('a', *x);
73 #[stable(feature = "rust1", since = "1.0.0")]
75 /// The resulting type after dereferencing.
76 #[stable(feature = "rust1", since = "1.0.0")]
79 /// Dereferences the value.
81 #[stable(feature = "rust1", since = "1.0.0")]
82 fn deref(&self) -> &Self::Target;
85 #[stable(feature = "rust1", since = "1.0.0")]
86 impl<T: ?Sized> Deref for &T {
89 fn deref(&self) -> &T { *self }
92 #[stable(feature = "rust1", since = "1.0.0")]
93 impl<T: ?Sized> Deref for &mut T {
96 fn deref(&self) -> &T { *self }
99 /// Used for mutable dereferencing operations, like in `*v = 1;`.
101 /// In addition to being used for explicit dereferencing operations with the
102 /// (unary) `*` operator in mutable contexts, `DerefMut` is also used implicitly
103 /// by the compiler in many circumstances. This mechanism is called
104 /// ['`Deref` coercion'][more]. In immutable contexts, [`Deref`] is used.
106 /// Implementing `DerefMut` for smart pointers makes mutating the data behind
107 /// them convenient, which is why they implement `DerefMut`. On the other hand,
108 /// the rules regarding [`Deref`] and `DerefMut` were designed specifically to
109 /// accommodate smart pointers. Because of this, **`DerefMut` should only be
110 /// implemented for smart pointers** to avoid confusion.
112 /// For similar reasons, **this trait should never fail**. Failure during
113 /// dereferencing can be extremely confusing when `DerefMut` is invoked
116 /// # More on `Deref` coercion
118 /// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
121 /// * In mutable contexts, `*x` on non-pointer types is equivalent to
122 /// `*Deref::deref(&x)`.
123 /// * Values of type `&mut T` are coerced to values of type `&mut U`
124 /// * `T` implicitly implements all the (mutable) methods of the type `U`.
126 /// For more details, visit [the chapter in *The Rust Programming Language*]
127 /// [book] as well as the reference sections on [the dereference operator]
128 /// [ref-deref-op], [method resolution] and [type coercions].
130 /// [book]: ../../book/second-edition/ch15-02-deref.html
131 /// [`Deref`]: trait.Deref.html
132 /// [more]: #more-on-deref-coercion
133 /// [ref-deref-op]: ../../reference/expressions/operator-expr.html#the-dereference-operator
134 /// [method resolution]: ../../reference/expressions/method-call-expr.html
135 /// [type coercions]: ../../reference/type-coercions.html
139 /// A struct with a single field which is modifiable by dereferencing the
143 /// use std::ops::{Deref, DerefMut};
145 /// struct DerefMutExample<T> {
149 /// impl<T> Deref for DerefMutExample<T> {
152 /// fn deref(&self) -> &T {
157 /// impl<T> DerefMut for DerefMutExample<T> {
158 /// fn deref_mut(&mut self) -> &mut T {
163 /// let mut x = DerefMutExample { value: 'a' };
165 /// assert_eq!('b', *x);
167 #[lang = "deref_mut"]
169 #[stable(feature = "rust1", since = "1.0.0")]
170 pub trait DerefMut: Deref {
171 /// Mutably dereferences the value.
172 #[stable(feature = "rust1", since = "1.0.0")]
173 fn deref_mut(&mut self) -> &mut Self::Target;
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl<T: ?Sized> DerefMut for &mut T {
178 fn deref_mut(&mut self) -> &mut T { *self }