Debian Codegraph @

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

raw
1 # What `#[derive(MulAssign)]` generates
2
3 This code is very similar to the code that is generated for `#[derive(Mul)]`.
4 The difference is that it mutates the existing instance instead of creating a
5 new one.
6
7 You can add the `#[mul_assign(forward)]` attribute if you don't want the same
8 semantics as `Mul`.
9 This will instead generate a `MulAssign` implementation with the same semantics
10 as `AddAssign`.
11
12
13
14
15 ## Tuple structs
16
17 When deriving `MulAssign` for a tuple struct with two fields like this:
18
19 ```rust
20 # use derive_more::MulAssign;
21 #
22 #[derive(MulAssign)]
23 struct MyInts(i32, i32);
24 ```
25
26 Code like this will be generated:
27
28 ```rust
29 # struct MyInts(i32, i32);
30 impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for MyInts
31 where i32: ::core::ops::MulAssign<__RhsT>
32 {
33 fn mul_assign(&mut self, rhs: __RhsT) {
34 self.0.mul_assign(rhs);
35 self.1.mul_assign(rhs);
36 }
37 }
38 ```
39
40 The behaviour is similar with more or less fields, except for the fact that
41 `__RhsT` does not need to implement `Copy` when only a single field is present.
42
43
44
45
46 ## Regular structs
47
48 When deriving `MulAssign` for a regular struct with two fields like this:
49
50 ```rust
51 # use derive_more::MulAssign;
52 #
53 #[derive(MulAssign)]
54 struct Point2D {
55 x: i32,
56 y: i32,
57 }
58 ```
59
60 Code like this will be generated:
61
62 ```rust
63 # struct Point2D {
64 # x: i32,
65 # y: i32,
66 # }
67 impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for Point2D
68 where i32: ::core::ops::MulAssign<__RhsT>
69 {
70 fn mul_assign(&mut self, rhs: __RhsT) {
71 self.x.mul_assign(rhs);
72 self.y.mul_assign(rhs);
73 }
74 }
75 ```
76
77 The behaviour is again similar with more or less fields, except that `Copy`
78 doesn't have to be implemented for `__Rhst` when the struct has only a single
79 field.
80
81
82
83
84 ## Enums
85
86 Deriving `MulAssign` for enums is not (yet) supported.
87 This has two reason, the first being that deriving `Mul` is also not implemented
88 for enums yet.
89 The second reason is the same as for `AddAssign`.
90 Even if it would be deriving `Mul` was implemented it would likely return a
91 `Result<EnumType>` instead of an `EnumType`.
92 Handling the case where it errors would be hard and maybe impossible.