| 1 |
# What `#[derive(DerefMut)]` generates |
| 2 |
|
| 3 |
Deriving `Deref` only works for a single field of a struct. |
| 4 |
Furthermore it requires that the type also implements `Deref`, so usually |
| 5 |
`Deref` should also be derived. |
| 6 |
The resulting implementation of `Deref` will allow you to mutably dereference |
| 7 |
the struct its member directly. |
| 8 |
|
| 9 |
1. Dereferencing to the field, i.e. like if your type was a reference type. |
| 10 |
2. Doing a dereference on the field, for when the field itself is a reference |
| 11 |
type like `&mut` and `Box`. |
| 12 |
|
| 13 |
With `#[deref_mut]` or `#[deref_mut(ignore)]` it's possible to indicate the |
| 14 |
field that you want to derive `DerefMut` for. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
## Example usage |
| 20 |
|
| 21 |
```rust |
| 22 |
# use derive_more::{Deref, DerefMut}; |
| 23 |
# |
| 24 |
#[derive(Deref, DerefMut)] |
| 25 |
struct Num { |
| 26 |
num: i32, |
| 27 |
} |
| 28 |
|
| 29 |
#[derive(Deref, DerefMut)] |
| 30 |
#[deref(forward)] |
| 31 |
#[deref_mut(forward)] |
| 32 |
struct MyBoxedInt(Box<i32>); |
| 33 |
|
| 34 |
// You can specify the field you want to derive DerefMut for |
| 35 |
#[derive(Deref, DerefMut)] |
| 36 |
struct CoolVec { |
| 37 |
cool: bool, |
| 38 |
#[deref] |
| 39 |
#[deref_mut] |
| 40 |
vec: Vec<i32>, |
| 41 |
} |
| 42 |
|
| 43 |
let mut num = Num{num: 123}; |
| 44 |
let mut boxed = MyBoxedInt(Box::new(123)); |
| 45 |
let mut cool_vec = CoolVec{cool: true, vec: vec![123]}; |
| 46 |
*num += 123; |
| 47 |
assert_eq!(246, *num); |
| 48 |
*boxed += 1000; |
| 49 |
assert_eq!(1123, *boxed); |
| 50 |
cool_vec.push(456); |
| 51 |
assert_eq!(vec![123, 456], *cool_vec); |
| 52 |
``` |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
## Structs |
| 58 |
|
| 59 |
When deriving a non-forwarded `Deref` for a struct: |
| 60 |
|
| 61 |
```rust |
| 62 |
# use derive_more::{Deref, DerefMut}; |
| 63 |
# |
| 64 |
#[derive(Deref, DerefMut)] |
| 65 |
struct CoolVec { |
| 66 |
cool: bool, |
| 67 |
#[deref] |
| 68 |
#[deref_mut] |
| 69 |
vec: Vec<i32>, |
| 70 |
} |
| 71 |
``` |
| 72 |
|
| 73 |
Code like this will be generated: |
| 74 |
|
| 75 |
```rust |
| 76 |
# struct CoolVec { |
| 77 |
# cool: bool, |
| 78 |
# vec: Vec<i32>, |
| 79 |
# } |
| 80 |
# impl ::core::ops::Deref for CoolVec { |
| 81 |
# type Target = Vec<i32>; |
| 82 |
# #[inline] |
| 83 |
# fn deref(&self) -> &Self::Target { |
| 84 |
# &self.vec |
| 85 |
# } |
| 86 |
# } |
| 87 |
impl ::core::ops::DerefMut for CoolVec { |
| 88 |
#[inline] |
| 89 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 90 |
&mut self.vec |
| 91 |
} |
| 92 |
} |
| 93 |
``` |
| 94 |
|
| 95 |
When deriving `DerefMut` for a tuple struct with one field: |
| 96 |
|
| 97 |
```rust |
| 98 |
# use derive_more::{Deref, DerefMut}; |
| 99 |
# |
| 100 |
#[derive(Deref, DerefMut)] |
| 101 |
#[deref(forward)] |
| 102 |
#[deref_mut(forward)] |
| 103 |
struct MyBoxedInt(Box<i32>); |
| 104 |
``` |
| 105 |
|
| 106 |
When deriving a forwarded `DerefMut` for a struct: |
| 107 |
|
| 108 |
```rust |
| 109 |
# struct MyBoxedInt(Box<i32>); |
| 110 |
# impl ::core::ops::Deref for MyBoxedInt { |
| 111 |
# type Target = <Box<i32> as ::core::ops::Deref>::Target; |
| 112 |
# #[inline] |
| 113 |
# fn deref(&self) -> &Self::Target { |
| 114 |
# <Box<i32> as ::core::ops::Deref>::deref(&self.0) |
| 115 |
# } |
| 116 |
# } |
| 117 |
impl ::core::ops::DerefMut for MyBoxedInt { |
| 118 |
#[inline] |
| 119 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 120 |
<Box<i32> as ::core::ops::DerefMut>::deref_mut(&mut self.0) |
| 121 |
} |
| 122 |
} |
| 123 |
``` |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
## Enums |
| 129 |
|
| 130 |
Deriving `DerefMut` is not supported for enums. |