| 1 | # What `#[derive(Mul)]` generates |
| 2 | |
| 3 | > **NOTE**: `Div`, `Rem`, `Shr` and `Shl` derives are fully equivalent |
| 4 | > to the `Mul` derive described below. |
| 5 | |
| 6 | In contrast with deriving `Add`, there are two ways to derive `Mul`: scalar and structural. |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | ## Scalar implementation |
| 12 | |
| 13 | By default, deriving `Mul` is quite different from deriving `Add`. It is not used |
| 14 | to multiply two structs together. Instead, it will normally multiply a struct, |
| 15 | which can have multiple fields, with a single primitive type (e.g. a `u64`). |
| 16 | A new struct is then created with all the fields from the previous struct |
| 17 | multiplied by this other value. |
| 18 | |
| 19 | A simple way of explaining the reasoning behind this difference between `Add` |
| 20 | and `Mul` deriving, is looking at arithmetic on meters. One meter can be added |
| 21 | to one meter, to get two meters. Also, one meter times two would be two meters, |
| 22 | but one meter times one meter would be one square meter. As this second case |
| 23 | clearly requires more knowledge about the meaning of the type in question |
| 24 | deriving for this is not implemented. |
| 25 | |
| 26 | |
| 27 | ### Structs |
| 28 | |
| 29 | Deriving `Mul` for a struct with a single field multiplies its field with |
| 30 | anything `Mul`tiplicable, producing the resulting struct. |
| 31 | |
| 32 | |
| 33 | ```rust |
| 34 | # use derive_more::Mul; |
| 35 | # |
| 36 | #[derive(Mul)] |
| 37 | struct MyInt(i32); |
| 38 | |
| 39 | #[derive(Mul)] |
| 40 | struct Point1D { |
| 41 | x: i32, |
| 42 | } |
| 43 | ``` |
| 44 | This generates code equivalent to: |
| 45 | ```rust |
| 46 | # use std::ops::Mul; |
| 47 | # |
| 48 | # struct MyInt(i32); |
| 49 | # |
| 50 | # struct Point1D { |
| 51 | # x: i32, |
| 52 | # } |
| 53 | # |
| 54 | impl<Rhs> Mul<Rhs> for MyInt |
| 55 | where |
| 56 | i32: Mul<Rhs, Output = i32> |
| 57 | { |
| 58 | type Output = Self; |
| 59 | |
| 60 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 61 | match self { |
| 62 | Self(self_0) => Self(Mul::mul(self_0, rhs)), |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl<Rhs> Mul<Rhs> for Point1D |
| 68 | where |
| 69 | i32: Mul<Rhs, Output = i32> |
| 70 | { |
| 71 | type Output = Self; |
| 72 | |
| 73 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 74 | match self { |
| 75 | Self { x: self_0 } => Self { x: Mul::mul(self_0, rhs) }, |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | The behaviour is slightly different for multiple fields, since the right hand |
| 82 | side of the multiplication now needs the `Copy` trait. |
| 83 | |
| 84 | ```rust |
| 85 | # use derive_more::Mul; |
| 86 | # |
| 87 | #[derive(Mul)] |
| 88 | struct MyInts(i32, i32); |
| 89 | |
| 90 | #[derive(Mul)] |
| 91 | struct Point2D { |
| 92 | x: i32, |
| 93 | y: i32, |
| 94 | } |
| 95 | ``` |
| 96 | This generates code equivalent to: |
| 97 | ```rust |
| 98 | # use std::ops::Mul; |
| 99 | # |
| 100 | # struct MyInts(i32, i32); |
| 101 | # |
| 102 | # struct Point2D { |
| 103 | # x: i32, |
| 104 | # y: i32, |
| 105 | # } |
| 106 | # |
| 107 | impl<Rhs: Copy> Mul<Rhs> for MyInts |
| 108 | where |
| 109 | i32: Mul<Rhs, Output = i32> |
| 110 | { |
| 111 | type Output = Self; |
| 112 | |
| 113 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 114 | match self { |
| 115 | Self(self_0, self_1) => { |
| 116 | Self(Mul::mul(self_0, rhs), Mul::mul(self_1, rhs)) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<Rhs: Copy> Mul<Rhs> for Point2D |
| 123 | where |
| 124 | i32: Mul<Rhs, Output = i32> |
| 125 | { |
| 126 | type Output = Self; |
| 127 | |
| 128 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 129 | match self { |
| 130 | Self { x: self_0, y: self_1 } => Self { |
| 131 | x: Mul::mul(self_0, rhs), |
| 132 | y: Mul::mul(self_1, rhs), |
| 133 | }, |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | #### Ignoring |
| 140 | |
| 141 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 142 | participate in a scalar `Mul` implementation. Such field could be ignored using |
| 143 | the `#[mul(skip)]` attribute. |
| 144 | |
| 145 | ```rust |
| 146 | # use core::marker::PhantomData; |
| 147 | # use derive_more::Mul; |
| 148 | # |
| 149 | #[derive(Mul)] |
| 150 | struct TupleWithZst<T>(i32, #[mul(skip)] PhantomData<T>); |
| 151 | |
| 152 | #[derive(Mul)] |
| 153 | struct StructWithZst<T> { |
| 154 | x: i32, |
| 155 | #[mul(skip)] // or #[mul(ignore)] |
| 156 | _marker: PhantomData<T>, |
| 157 | } |
| 158 | ``` |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | ### Enums |
| 164 | |
| 165 | Deriving scalar `Mul` implementation for enums is not (yet) supported. |
| 166 | |
| 167 | Although it shouldn't be impossible no effort has been put into this yet. |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | ## Structural implementation |
| 173 | |
| 174 | Specifying the `#[mul(forward)]` attribute generates a structural `Mul` |
| 175 | implementation with the same semantics as `Add`: `Mul`tiplying respective |
| 176 | fields together and producing a new value with these fields. |
| 177 | |
| 178 | |
| 179 | ### Structs |
| 180 | |
| 181 | ```rust |
| 182 | # use derive_more::Mul; |
| 183 | # |
| 184 | #[derive(Mul)] |
| 185 | #[mul(forward)] |
| 186 | struct MyInts(i32, i32); |
| 187 | |
| 188 | #[derive(Mul)] |
| 189 | #[mul(forward)] |
| 190 | struct Point2D { |
| 191 | x: i32, |
| 192 | y: i32, |
| 193 | } |
| 194 | ``` |
| 195 | This generates code equivalent to: |
| 196 | ```rust |
| 197 | # use std::ops::Mul; |
| 198 | # |
| 199 | # struct MyInts(i32, i32); |
| 200 | # |
| 201 | # struct Point2D { |
| 202 | # x: i32, |
| 203 | # y: i32, |
| 204 | # } |
| 205 | # |
| 206 | impl Mul for MyInts { |
| 207 | type Output = Self; |
| 208 | |
| 209 | fn mul(self, rhs: Self) -> Self::Output { |
| 210 | match (self, rhs) { |
| 211 | (Self(self_0, self_1), Self(rhs_0, rhs_1)) => { |
| 212 | Self(Mul::mul(self_0, rhs_0), Mul::mul(self_1, rhs_1)) |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl Mul for Point2D { |
| 219 | type Output = Self; |
| 220 | |
| 221 | fn mul(self, rhs: Self) -> Self::Output { |
| 222 | match (self, rhs) { |
| 223 | (Self { x: self_0, y: self_1 }, Self { x: rhs_0, y: rhs_1 }) => { |
| 224 | Self { x: Mul::mul(self_0, rhs_0), y: Mul::mul(self_1, rhs_1) } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | ``` |
| 230 | The behaviour is similar with more or less fields. |
| 231 | |
| 232 | #### Ignoring |
| 233 | |
| 234 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 235 | participate in a structural `Mul` implementation. Such field could be ignored using |
| 236 | the `#[mul(skip)]` attribute. |
| 237 | |
| 238 | ```rust |
| 239 | # use core::marker::PhantomData; |
| 240 | # use derive_more::Mul; |
| 241 | # |
| 242 | #[derive(Mul)] |
| 243 | #[mul(forward)] |
| 244 | struct TupleWithZst<T>(i32, #[mul(skip)] PhantomData<T>); |
| 245 | |
| 246 | #[derive(Mul)] |
| 247 | #[mul(forward)] |
| 248 | struct StructWithZst<T> { |
| 249 | x: i32, |
| 250 | #[mul(skip)] // or #[mul(ignore)] |
| 251 | _marker: PhantomData<T>, |
| 252 | } |
| 253 | ``` |
| 254 | |
| 255 | |
| 256 | ### Enums |
| 257 | |
| 258 | For enums each variant can be `Mul`tiplied structurally in a similar way to another |
| 259 | instance of the same variant. There's one big difference however: it returns |
| 260 | a `Result<EnumType>`, because an error is returned when two different variants are |
| 261 | `Mul`tiplied together. |
| 262 | |
| 263 | ```rust |
| 264 | # use derive_more::Mul; |
| 265 | # |
| 266 | #[derive(Mul)] |
| 267 | #[mul(forward)] |
| 268 | enum MixedInts { |
| 269 | BigInt(i64), |
| 270 | NamedSmallInts { x: i32, y: i32 }, |
| 271 | Unit, |
| 272 | } |
| 273 | ``` |
| 274 | This generates code equivalent to: |
| 275 | ```rust |
| 276 | # use std::ops::Mul; |
| 277 | # |
| 278 | # enum MixedInts { |
| 279 | # BigInt(i64), |
| 280 | # NamedSmallInts { x: i32, y: i32 }, |
| 281 | # Unit, |
| 282 | # } |
| 283 | # |
| 284 | impl Mul for MixedInts { |
| 285 | type Output = Result<Self, derive_more::BinaryError>; |
| 286 | |
| 287 | fn mul(self, rhs: Self) -> Self::Output { |
| 288 | match (self, rhs) { |
| 289 | (Self::BigInt(self_0), Self::BigInt(rhs_0)) => { |
| 290 | Ok(Self::BigInt(Mul::mul(self_0, rhs_0))) |
| 291 | } |
| 292 | (Self::NamedSmallInts { x: self_0, y: self_1 }, |
| 293 | Self::NamedSmallInts { x: rhs_0, y: rhs_1 }) => { |
| 294 | Ok(Self::NamedSmallInts { |
| 295 | x: Mul::mul(self_0, rhs_0), |
| 296 | y: Mul::mul(self_1, rhs_1), |
| 297 | }) |
| 298 | } |
| 299 | (Self::Unit, Self::Unit) => Err(derive_more::BinaryError::Unit( |
| 300 | derive_more::UnitError::new("add"), |
| 301 | )), |
| 302 | _ => Err(derive_more::BinaryError::Mismatch( |
| 303 | derive_more::WrongVariantError::new("add"), |
| 304 | )), |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | ``` |
| 309 | |
| 310 | Also note the `Unit` variant that throws a `derive_more::UnitError` when `Mul`tiplying it |
| 311 | to itself. |
| 312 | |
| 313 | #### Ignoring |
| 314 | |
| 315 | Similarly to structs, enum fields could be ignored using the `#[add(skip)]` attribute. |
| 316 | |
| 317 | ```rust |
| 318 | # use derive_more::Mul; |
| 319 | # |
| 320 | struct I32(i32); // doesn't implement `Mul` |
| 321 | |
| 322 | #[derive(Mul)] |
| 323 | #[mul(forward)] |
| 324 | enum MixedInts { |
| 325 | TwoSmallInts(i32, #[mul(skip)] I32), |
| 326 | NamedSmallInts { |
| 327 | #[mul(skip)] // or #[mul(ignore)] |
| 328 | x: I32, |
| 329 | y: i32, |
| 330 | }, |
| 331 | } |
| 332 | ``` |
| 333 | |
| 334 | > **NOTE**: Ignoring all the fields of a variant or ignoring the variant itself is not allowed |
| 335 | > (results in a compilation error). |