Debian Codegraph @

third_party/rust/derive_more-impl/doc/as_mut.md

raw
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:
50
51 ```rust
52 # struct SingleFieldForward(Vec<i32>);
53 impl<__AsMutT: ?::core::marker::Sized> ::core::convert::AsMut<__AsMutT> for SingleFieldForward
54 where
55 Vec<i32>: ::core::convert::AsMut<__AsMutT>,
56 {
57 #[inline]
58 fn as_mut(&mut self) -> &mut __AsMutT {
59 <Vec<i32> as ::core::convert::AsMut<__AsMutT>>::as_mut(&mut self.0)
60 }
61 }
62 ```
63
64
65
66
67 ## Structs with Multiple Fields
68
69 When `AsMut` is derived for a struct with more than one field (including tuple
70 structs), you must also mark one or more fields with the `#[as_mut]` attribute.
71 An implementation will be generated for each indicated field.
72 You can also exclude a specific field by using `#[as_mut(ignore)]`.
73
74 ```rust
75 # use derive_more::AsMut;
76 #
77 #[derive(AsMut)]
78 struct MyWrapper {
79 #[as_mut]
80 name: String,
81 #[as_mut]
82 num: i32,
83 valid: bool,
84 }
85 ```
86
87 Generates:
88
89 ```rust
90 # struct MyWrapper {
91 # name: String,
92 # num: i32,
93 # valid: bool,
94 # }
95 impl AsMut<String> for MyWrapper {
96 fn as_mut(&mut self) -> &mut String {
97 &mut self.name
98 }
99 }
100
101 impl AsMut<i32> for MyWrapper {
102 fn as_mut(&mut self) -> &mut i32 {
103 &mut self.num
104 }
105 }
106 ```
107
108 Note that `AsMut<T>` may only be implemented once for any given type `T`. This means any attempt to
109 mark more than one field of the same type with `#[as_mut]` will result in a compilation error.
110
111 ```rust,compile_fail
112 # use derive_more::AsMut;
113 #
114 // Error! Conflicting implementations of AsMut<String>
115 #[derive(AsMut)]
116 struct MyWrapper {
117 #[as_mut]
118 str1: String,
119 #[as_mut]
120 str2: String,
121 }
122 ```
123
124
125
126
127 ## Enums
128
129 Deriving `AsMut` for enums is not supported.