Debian Codegraph @

debian/missing-sources/derive_more-impl/doc/as_mut.md

raw symbols SCIP
1 # What `#[derive(AsMut)]` generates
2
3 Deriving `AsMut` generates one or more implementations of `AsMut`, each
4 corresponding to one of the fields of the decorated type.
5 This allows types which contain some `T` to be passed anywhere that an
6 `AsMut<T>` is accepted.
7
8
9
10
11 ## Newtypes and Structs with One Field
12
13 When `AsMut` is derived for a newtype or struct with one field, a single
14 implementation is generated to expose the underlying field.
15
16 ```rust
17 # use derive_more::AsMut;
18 #
19 #[derive(AsMut)]
20 struct MyWrapper(String);
21 ```
22
23 Generates:
24
25 ```rust
26 # struct MyWrapper(String);
27 impl AsMut<String> for MyWrapper {
28 fn as_mut(&mut self) -> &mut String {
29 &mut self.0
30 }
31 }
32 ```
33
34 It's also possible to use the `#[as_mut(forward)]` attribute to forward
35 to the `as_mut` implementation of the field. So here `SingleFieldForward`
36 implements all `AsMut` for all types that `Vec<i32>` implements `AsMut` for.
37
38 ```rust
39 # use derive_more::AsMut;
40 #
41 #[derive(AsMut)]
42 #[as_mut(forward)]
43 struct SingleFieldForward(Vec<i32>);
44
45 let mut item = SingleFieldForward(vec![]);
46 let _: &mut [i32] = (&mut item).as_mut();
47 ```
48
49 This generates code equivalent to:
50
51 ```rust
52 # struct SingleFieldForward(Vec<i32>);
53 impl<T: ?Sized> AsMut<T> for SingleFieldForward
54 where
55 Vec<i32>: AsMut<T>,
56 {
57 #[inline]
58 fn as_mut(&mut self) -> &mut T {
59 self.0.as_mut()
60 }
61 }
62 ```
63
64 Specifying concrete types, to derive impls for, is also supported via
65 `#[as_mut(<types>)]` attribute. These types can include both the type
66 of the field itself, and types for which the field type implements `AsMut`.
67
68 ```rust
69 # use derive_more::AsMut;
70 #
71 #[derive(AsMut)]
72 #[as_mut(str, [u8], String)]
73 struct Types(String);
74
75 let mut item = Types("test".to_owned());
76 let _: &mut str = item.as_mut();
77 let _: &mut [u8] = item.as_mut();
78 let _: &mut String = item.as_mut();_
79 ```
80
81 > **WARNING**: When either the field type, or the specified conversion type,
82 > contains generic parameters, they are considered as the same type only if
83 > are named string-equally, otherwise are assumed as different types even
84 > when represent the same type in fact (type aliases, for example).
85 >
86 > ```rust
87 > # use derive_more::AsMut;
88 > #
89 > #[derive(AsMut)]
90 > #[as_mut(i32)] // generates `impl<T: AsMut<i32>> AsMut<i32> for Generic<T>`
91 > struct Generic<T>(T);
92 >
93 > #[derive(AsMut)]
94 > #[as_mut(T)] // generates `impl<T> AsMut<T> for Transparent<T>`
95 > struct Transparent<T>(T);
96 >
97 > #[derive(AsMut)]
98 > // #[as_mut(RenamedVec<T>)] // not supported, as types are not named string-equally
99 > struct Foo<T>(Vec<T>);
100 > type RenamedVec<T> = Vec<T>;
101 >
102 > #[derive(AsMut)]
103 > #[as_mut(RenamedString)] // generates `impl AsMut<RenamedString> for Bar`,
104 > struct Bar(String); // as generics are not involved
105 > type RenamedString = String;
106 > ```
107
108 Generating code like this is not supported:
109
110 ```rust
111 struct Generic<T>(T);
112
113 impl AsMut<i32> for Generic<i32> {
114 fn as_mut(&mut self) -> &mut i32 {
115 &mut self.0
116 }
117 }
118 ```
119
120
121
122
123 ## Structs with Multiple Fields
124
125 When `AsMut` is derived for a struct with more than one field (including tuple
126 structs), you must also mark one or more fields with the `#[as_mut]` attribute.
127 An implementation will be generated for each indicated field.
128
129 ```rust
130 # use derive_more::AsMut;
131 #
132 #[derive(AsMut)]
133 struct MyWrapper {
134 #[as_mut(str)]
135 name: String,
136 #[as_mut]
137 num: i32,
138 valid: bool,
139 }
140 ```
141
142 Generates:
143
144 ```rust
145 # struct MyWrapper {
146 # name: String,
147 # num: i32,
148 # valid: bool,
149 # }
150 impl AsMut<str> for MyWrapper {
151 fn as_mut(&mut self) -> &mut String {
152 self.name.as_mut()
153 }
154 }
155
156 impl AsMut<i32> for MyWrapper {
157 fn as_mut(&mut self) -> &mut i32 {
158 &mut self.num
159 }
160 }
161 ```
162
163
164 ### Tuples (not supported)
165
166 Only conversions that use a single field are possible with this derive.
167 Something like this wouldn't work, due to the nature of the `AsMut` trait
168 itself:
169
170 ```rust,compile_fail
171 # use derive_more::AsMut
172 #
173 #[derive(AsMut)]
174 #[as_mut((str, [u8]))]
175 struct MyWrapper(String, Vec<u8>)
176 ```
177
178 If you need to convert into a tuple of references, consider using the
179 [`Into`](crate::Into) derive with `#[into(ref_mut)]`.
180
181
182 ### Skipping
183
184 Or vice versa: you can exclude a specific field by using `#[as_mut(skip)]` (or
185 `#[as_mut(ignore)]`). Then, implementations will be generated for non-indicated fields.
186
187 ```rust
188 # use derive_more::AsMut;
189 #
190 #[derive(AsMut)]
191 struct MyWrapper {
192 #[as_mut(skip)]
193 name: String,
194 #[as_mut(ignore)]
195 num: i32,
196 valid: bool,
197 }
198 ```
199
200 Generates:
201
202 ```rust
203 # struct MyWrapper {
204 # name: String,
205 # num: i32,
206 # valid: bool,
207 # }
208 impl AsMut<bool> for MyWrapper {
209 fn as_mut(&mut self) -> &mut bool {
210 &mut self.valid
211 }
212 }
213 ```
214
215
216 ### Coherence
217
218 Note that `AsMut<T>` may only be implemented once for any given type `T`.
219 This means any attempt to mark more than one field of the same type with
220 `#[as_mut]` will result in a compilation error.
221
222 ```rust,compile_fail
223 # use derive_more::AsMut;
224 #
225 // Error! Conflicting implementations of AsMut<String>
226 #[derive(AsMut)]
227 struct MyWrapper {
228 #[as_mut]
229 str1: String,
230 #[as_mut]
231 str2: String,
232 }
233 ```
234
235 Similarly, if some field is annotated with `#[as_mut(forward)]`, no other
236 field can be marked.
237
238 ```rust,compile_fail
239 # use derive_more::AsMut;
240 #
241 // Error! Conflicting implementations of `AsMut<i32>`
242 // note: upstream crates may add a new impl of trait `AsMut<i32>`
243 // for type `String` in future versions
244 #[derive(AsMut)]
245 struct ForwardWithOther {
246 #[as_mut(forward)]
247 str: String,
248 #[as_mut]
249 number: i32,
250 }
251 ```
252
253 Multiple forwarded impls with different concrete types, however, can be used.
254
255 ```rust
256 # use derive_more::AsMut;
257 #
258 #[derive(AsMut)]
259 struct Types {
260 #[as_mut(str)]
261 str: String,
262 #[as_mut([u8])]
263 vec: Vec<u8>,
264 }
265
266 let mut item = Types {
267 str: "test".to_owned(),
268 vec: vec![0u8],
269 };
270
271 let _: &mut str = item.as_mut();
272 let _: &mut [u8] = item.as_mut();
273 ```
274
275
276
277
278 ## Enums
279
280 Deriving `AsMut` for enums is not supported.