| 1 | # What `#[derive(DerefMut)]` generates |
| 2 | |
| 3 | Deriving `Deref` only works for a single field of a struct, or a single field of |
| 4 | each variant of an enum. |
| 5 | Furthermore it requires that the type also implements `Deref`, so usually |
| 6 | `Deref` should also be derived. |
| 7 | The resulting implementation of `Deref` will allow you to mutably dereference |
| 8 | the struct its member directly. |
| 9 | |
| 10 | 1. Dereferencing to the field, i.e. like if your type was a reference type. |
| 11 | 2. Doing a dereference on the field, for when the field itself is a reference |
| 12 | type like `&mut` and `Box`. |
| 13 | |
| 14 | With `#[deref_mut]` or `#[deref_mut(ignore)]` it's possible to indicate the |
| 15 | field that you want to derive `DerefMut` for. |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | ## Example usage |
| 21 | |
| 22 | ```rust |
| 23 | # use derive_more::{Deref, DerefMut}; |
| 24 | # |
| 25 | #[derive(Deref, DerefMut)] |
| 26 | struct Num { |
| 27 | num: i32, |
| 28 | } |
| 29 | |
| 30 | #[derive(Deref, DerefMut)] |
| 31 | enum Enum { |
| 32 | V1(i32), |
| 33 | V2 { num: i32 }, |
| 34 | } |
| 35 | |
| 36 | #[derive(Deref, DerefMut)] |
| 37 | #[deref(forward)] |
| 38 | #[deref_mut(forward)] |
| 39 | struct MyBoxedInt(Box<i32>); |
| 40 | |
| 41 | #[derive(Deref, DerefMut)] |
| 42 | #[deref(forward)] |
| 43 | #[deref_mut(forward)] |
| 44 | enum MyBoxedIntEnum { |
| 45 | V1(Box<i32>), |
| 46 | V2 { num: Box<i32> }, |
| 47 | } |
| 48 | |
| 49 | // You can specify the field you want to derive DerefMut for |
| 50 | #[derive(Deref, DerefMut)] |
| 51 | struct CoolVec { |
| 52 | cool: bool, |
| 53 | #[deref] |
| 54 | #[deref_mut] |
| 55 | vec: Vec<i32>, |
| 56 | } |
| 57 | |
| 58 | #[derive(Deref, DerefMut)] |
| 59 | enum CoolVecEnum { |
| 60 | V1(Vec<i32>), |
| 61 | V2 { |
| 62 | cool: bool, |
| 63 | #[deref] |
| 64 | #[deref_mut] |
| 65 | vec: Vec<i32>, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | let mut num = Num{num: 123}; |
| 70 | let mut boxed = MyBoxedInt(Box::new(123)); |
| 71 | let mut cool_vec = CoolVec{cool: true, vec: vec![123]}; |
| 72 | *num += 123; |
| 73 | assert_eq!(246, *num); |
| 74 | *boxed += 1000; |
| 75 | assert_eq!(1123, *boxed); |
| 76 | cool_vec.push(456); |
| 77 | assert_eq!(vec![123, 456], *cool_vec); |
| 78 | ``` |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | ## Structs |
| 84 | |
| 85 | When deriving a non-forwarded `DerefMut` for a struct: |
| 86 | |
| 87 | ```rust |
| 88 | # use derive_more::{Deref, DerefMut}; |
| 89 | # |
| 90 | #[derive(Deref, DerefMut)] |
| 91 | struct CoolVec { |
| 92 | cool: bool, |
| 93 | #[deref] |
| 94 | #[deref_mut] |
| 95 | vec: Vec<i32>, |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | Code like this will be generated: |
| 100 | |
| 101 | ```rust |
| 102 | # use ::core::ops::Deref; |
| 103 | # struct CoolVec { |
| 104 | # cool: bool, |
| 105 | # vec: Vec<i32>, |
| 106 | # } |
| 107 | # impl Deref for CoolVec { |
| 108 | # type Target = Vec<i32>; |
| 109 | # #[inline] |
| 110 | # fn deref(&self) -> &Self::Target { |
| 111 | # &self.vec |
| 112 | # } |
| 113 | # } |
| 114 | impl derive_more::core::ops::DerefMut for CoolVec { |
| 115 | #[inline] |
| 116 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 117 | &mut self.vec |
| 118 | } |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | When deriving `DerefMut` for a tuple struct with one field: |
| 123 | |
| 124 | ```rust |
| 125 | # use derive_more::{Deref, DerefMut}; |
| 126 | # |
| 127 | #[derive(Deref, DerefMut)] |
| 128 | #[deref(forward)] |
| 129 | #[deref_mut(forward)] |
| 130 | struct MyBoxedInt(Box<i32>); |
| 131 | ``` |
| 132 | |
| 133 | When deriving a forwarded `DerefMut` for a struct: |
| 134 | |
| 135 | ```rust |
| 136 | # use ::core::ops::Deref; |
| 137 | # struct MyBoxedInt(Box<i32>); |
| 138 | # impl Deref for MyBoxedInt { |
| 139 | # type Target = <Box<i32> as Deref>::Target; |
| 140 | # #[inline] |
| 141 | # fn deref(&self) -> &Self::Target { |
| 142 | # <Box<i32> as Deref>::deref(&self.0) |
| 143 | # } |
| 144 | # } |
| 145 | impl derive_more::core::ops::DerefMut for MyBoxedInt { |
| 146 | #[inline] |
| 147 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 148 | <Box<i32> as derive_more::core::ops::DerefMut>::deref_mut(&mut self.0) |
| 149 | } |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | ## Enums |
| 157 | |
| 158 | When deriving a non-forwarded `DerefMut` for an enum: |
| 159 | |
| 160 | ```rust |
| 161 | # use derive_more::{Deref, DerefMut}; |
| 162 | # |
| 163 | #[derive(Deref, DerefMut)] |
| 164 | enum CoolVecEnum { |
| 165 | V1(Vec<i32>), |
| 166 | V2 { |
| 167 | cool: bool, |
| 168 | #[deref] |
| 169 | #[deref_mut] |
| 170 | vec: Vec<i32>, |
| 171 | }, |
| 172 | } |
| 173 | ``` |
| 174 | |
| 175 | Code like this will be generated: |
| 176 | |
| 177 | ```rust |
| 178 | # use ::core::ops::Deref; |
| 179 | # enum CoolVecEnum { |
| 180 | # V1(Vec<i32>), |
| 181 | # V2 { |
| 182 | # cool: bool, |
| 183 | # vec: Vec<i32>, |
| 184 | # }, |
| 185 | # } |
| 186 | # impl Deref for CoolVecEnum { |
| 187 | # type Target = Vec<i32>; |
| 188 | # #[inline] |
| 189 | # fn deref(&self) -> &Self::Target { |
| 190 | # match self { |
| 191 | # CoolVecEnum::V1(__0) => __0, |
| 192 | # CoolVecEnum::V2 { cool: _, vec: __0 } => __0, |
| 193 | # } |
| 194 | # } |
| 195 | # } |
| 196 | impl derive_more::with_trait::DerefMut for CoolVecEnum { |
| 197 | #[inline] |
| 198 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 199 | match self { |
| 200 | CoolVecEnum::V1(__0) => __0, |
| 201 | CoolVecEnum::V2 { cool: _, vec: __0 } => __0, |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | ``` |
| 206 | |
| 207 | When deriving a forwarded `DerefMut` for an enum: |
| 208 | |
| 209 | ```rust |
| 210 | # use derive_more::{Deref, DerefMut}; |
| 211 | # |
| 212 | #[derive(Deref, DerefMut)] |
| 213 | #[deref(forward)] |
| 214 | #[deref_mut(forward)] |
| 215 | enum MyBoxedIntEnum { |
| 216 | V1(Box<i32>), |
| 217 | V2 { num: Box<i32> }, |
| 218 | } |
| 219 | ``` |
| 220 | |
| 221 | Code like this will be generated: |
| 222 | |
| 223 | ```rust |
| 224 | # use ::core::ops::Deref; |
| 225 | # enum MyBoxedIntEnum { |
| 226 | # V1(Box<i32>), |
| 227 | # V2 { num: Box<i32> }, |
| 228 | # } |
| 229 | # impl Deref for MyBoxedIntEnum { |
| 230 | # type Target = <Box<i32> as derive_more::with_trait::Deref>::Target; |
| 231 | # #[inline] |
| 232 | # fn deref(&self) -> &Self::Target { |
| 233 | # match self { |
| 234 | # MyBoxedIntEnum::V1(__0) => |
| 235 | # <Box<i32> as derive_more::with_trait::Deref>::deref(__0), |
| 236 | # MyBoxedIntEnum::V2 { num: __0 } => |
| 237 | # <Box<i32> as derive_more::with_trait::Deref>::deref(__0), |
| 238 | # } |
| 239 | # } |
| 240 | # } |
| 241 | impl derive_more::with_trait::DerefMut for MyBoxedIntEnum { |
| 242 | #[inline] |
| 243 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 244 | match self { |
| 245 | MyBoxedIntEnum::V1(__0) => { |
| 246 | <Box<i32> as derive_more::with_trait::DerefMut>::deref_mut(__0) |
| 247 | } |
| 248 | MyBoxedIntEnum::V2 { num: __0 } => { |
| 249 | <Box<i32> as derive_more::with_trait::DerefMut>::deref_mut(__0) |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | ``` |