| 1 | # What `#[derive(MulAssign)]` generates |
| 2 | |
| 3 | > **NOTE**: `DivAssign`, `RemAssign`, `ShrAssign` and `ShlAssign` derives are fully |
| 4 | > equivalent to the `MulAssign` derive described below. |
| 5 | |
| 6 | Deriving `MulAssign` is very similar to deriving `Mul`. The difference is that it |
| 7 | mutates the existing instance instead of creating a new one. |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | ## Scalar implementation |
| 13 | |
| 14 | |
| 15 | ### Structs |
| 16 | |
| 17 | Deriving `MulAssign` for a struct with multiple fields multiplies its fields with |
| 18 | anything multiplicable, mutating them in-place. |
| 19 | |
| 20 | ```rust |
| 21 | # use derive_more::MulAssign; |
| 22 | # |
| 23 | #[derive(MulAssign)] |
| 24 | struct MyInts(i32, i32); |
| 25 | |
| 26 | #[derive(MulAssign)] |
| 27 | struct Point2D { |
| 28 | x: i32, |
| 29 | y: i32, |
| 30 | } |
| 31 | ``` |
| 32 | This generates code equivalent to: |
| 33 | ```rust |
| 34 | # use std::ops::MulAssign; |
| 35 | # |
| 36 | # struct MyInts(i32, i32); |
| 37 | # |
| 38 | # struct Point2D { |
| 39 | # x: i32, |
| 40 | # y: i32, |
| 41 | # } |
| 42 | # |
| 43 | impl<Rhs: Copy> MulAssign<Rhs> for MyInts |
| 44 | where |
| 45 | i32: MulAssign<Rhs> |
| 46 | { |
| 47 | fn mul_assign(&mut self, rhs: Rhs) { |
| 48 | match self { |
| 49 | Self(self_0, self_1) => { |
| 50 | MulAssign::mul_assign(self_0, rhs); |
| 51 | MulAssign::mul_assign(self_1, rhs); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl<Rhs: Copy> MulAssign<Rhs> for Point2D |
| 58 | where |
| 59 | i32: MulAssign<Rhs> |
| 60 | { |
| 61 | fn mul_assign(&mut self, rhs: Rhs) { |
| 62 | match self { |
| 63 | Self { x: self_0, y: self_1 } => { |
| 64 | MulAssign::mul_assign(self_0, rhs); |
| 65 | MulAssign::mul_assign(self_1, rhs); |
| 66 | }, |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | Note, that `Copy`is not required for `Rhs` when the struct has only a single field. |
| 73 | |
| 74 | #### Ignoring |
| 75 | |
| 76 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 77 | participate in a scalar `MulAssign` implementation. Such field could be ignored using |
| 78 | the `#[mul_assign(skip)]` attribute. |
| 79 | |
| 80 | ```rust |
| 81 | # use core::marker::PhantomData; |
| 82 | # use derive_more::MulAssign; |
| 83 | # |
| 84 | #[derive(MulAssign)] |
| 85 | struct TupleWithZst<T>(i32, #[mul_assign(skip)] PhantomData<T>); |
| 86 | |
| 87 | #[derive(MulAssign)] |
| 88 | struct StructWithZst<T> { |
| 89 | x: i32, |
| 90 | #[mul_assign(skip)] // or #[mul_assign(ignore)] |
| 91 | _marker: PhantomData<T>, |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | |
| 96 | ### Enums |
| 97 | |
| 98 | Deriving scalar `MulAssign` implementation for enums is not (yet) supported (in the same manner as deriving `Mul`). |
| 99 | |
| 100 | Although it shouldn't be impossible no effort has been put into this yet. |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | ## Structural implementation |
| 106 | |
| 107 | Specifying the `#[mul_assign(forward)]` attribute generates a structural `MulAssign` |
| 108 | implementation with the same semantics as `AddAssign`: `MulAssign`ing the respective |
| 109 | fields, mutating them in-place. |
| 110 | |
| 111 | |
| 112 | ### Structs |
| 113 | |
| 114 | ```rust |
| 115 | # use derive_more::MulAssign; |
| 116 | # |
| 117 | #[derive(MulAssign)] |
| 118 | #[mul_assign(forward)] |
| 119 | struct MyInts(i32, i32); |
| 120 | |
| 121 | #[derive(MulAssign)] |
| 122 | #[mul_assign(forward)] |
| 123 | struct Point2D { |
| 124 | x: i32, |
| 125 | y: i32, |
| 126 | } |
| 127 | ``` |
| 128 | This generates code equivalent to: |
| 129 | ```rust |
| 130 | # use std::ops::MulAssign; |
| 131 | # |
| 132 | # struct MyInts(i32, i32); |
| 133 | # |
| 134 | # struct Point2D { |
| 135 | # x: i32, |
| 136 | # y: i32, |
| 137 | # } |
| 138 | # |
| 139 | impl MulAssign for MyInts { |
| 140 | fn mul_assign(&mut self, rhs: Self) { |
| 141 | match (self, rhs) { |
| 142 | (Self(self_0, self_1), Self(rhs_0, rhs_1)) => { |
| 143 | MulAssign::mul_assign(self_0, rhs_0); |
| 144 | MulAssign::mul_assign(self_1, rhs_1); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl MulAssign for Point2D { |
| 151 | fn mul_assign(&mut self, rhs: Self) { |
| 152 | match (self, rhs) { |
| 153 | (Self { x: self_0, y: self_1 }, Self { x: rhs_0, y: rhs_1 }) => { |
| 154 | MulAssign::mul_assign(self_0, rhs_0); |
| 155 | MulAssign::mul_assign(self_1, rhs_1); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | ``` |
| 161 | The behaviour is similar with more or less fields. |
| 162 | |
| 163 | #### Ignoring |
| 164 | |
| 165 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 166 | participate in a structural `MulAssign` implementation. Such field could be ignored using |
| 167 | the `#[mul_assign(skip)]` attribute. |
| 168 | |
| 169 | ```rust |
| 170 | # use core::marker::PhantomData; |
| 171 | # use derive_more::MulAssign; |
| 172 | # |
| 173 | #[derive(MulAssign)] |
| 174 | #[mul_assign(forward)] |
| 175 | struct TupleWithZst<T>(i32, #[mul_assign(skip)] PhantomData<T>); |
| 176 | |
| 177 | #[derive(MulAssign)] |
| 178 | #[mul_assign(forward)] |
| 179 | struct StructWithZst<T> { |
| 180 | x: i32, |
| 181 | #[mul_assign(skip)] // or #[mul_assign(ignore)] |
| 182 | _marker: PhantomData<T>, |
| 183 | } |
| 184 | ``` |
| 185 | |
| 186 | |
| 187 | ### Enums |
| 188 | |
| 189 | Deriving `AddAssign` structurally is not (yet) supported for enums. |
| 190 | |
| 191 | This is mostly due to the fact that it is not trivial convert the `Mul` |
| 192 | derivation code, because that returns a `Result<EnumType>` instead of an `EnumType`. |
| 193 | Handling the case where it errors would be hard and maybe impossible. |