| 1 |
# What `#[derive(Mul)]` generates |
| 2 |
|
| 3 |
Deriving `Mul` is quite different from deriving `Add`. It is not used to |
| 4 |
multiply two structs together. Instead it will normally multiply a struct, which |
| 5 |
can have multiple fields, with a single primitive type (e.g. a `u64`). A new |
| 6 |
struct is then created with all the fields from the previous struct multiplied |
| 7 |
by this other value. |
| 8 |
|
| 9 |
A simple way of explaining the reasoning behind this difference between `Add` |
| 10 |
and `Mul` deriving, is looking at arithmetic on meters. |
| 11 |
One meter can be added to one meter, to get two meters. Also, one meter times |
| 12 |
two would be two meters, but one meter times one meter would be one square meter. |
| 13 |
As this second case clearly requires more knowledge about the meaning of the |
| 14 |
type in question deriving for this is not implemented. |
| 15 |
|
| 16 |
NOTE: In case you don't want this behaviour you can add `#[mul(forward)]` in |
| 17 |
addition to `#[derive(Mul)]`. This will instead generate a `Mul` implementation |
| 18 |
with the same semantics as `Add`. |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
## Tuple structs |
| 24 |
|
| 25 |
When deriving for a tuple struct with a single field (i.e. a newtype) like this: |
| 26 |
|
| 27 |
```rust |
| 28 |
# use derive_more::Mul; |
| 29 |
# |
| 30 |
#[derive(Mul)] |
| 31 |
struct MyInt(i32); |
| 32 |
``` |
| 33 |
|
| 34 |
Code like this will be generated: |
| 35 |
|
| 36 |
```rust |
| 37 |
# struct MyInt(i32); |
| 38 |
impl<__RhsT> ::core::ops::Mul<__RhsT> for MyInt |
| 39 |
where i32: ::core::ops::Mul<__RhsT, Output = i32> |
| 40 |
{ |
| 41 |
type Output = MyInt; |
| 42 |
fn mul(self, rhs: __RhsT) -> MyInt { |
| 43 |
MyInt(self.0.mul(rhs)) |
| 44 |
} |
| 45 |
} |
| 46 |
``` |
| 47 |
|
| 48 |
The behaviour is slightly different for multiple fields, since the right hand |
| 49 |
side of the multiplication now needs the `Copy` trait. |
| 50 |
For instance when deriving for a tuple struct with two fields like this: |
| 51 |
|
| 52 |
```rust |
| 53 |
# use derive_more::Mul; |
| 54 |
# |
| 55 |
#[derive(Mul)] |
| 56 |
struct MyInts(i32, i32); |
| 57 |
``` |
| 58 |
|
| 59 |
Code like this will be generated: |
| 60 |
|
| 61 |
```rust |
| 62 |
# struct MyInts(i32, i32); |
| 63 |
impl<__RhsT: ::core::marker::Copy> ::core::ops::Mul<__RhsT> for MyInts |
| 64 |
where i32: ::core::ops::Mul<__RhsT, Output = i32> |
| 65 |
{ |
| 66 |
type Output = MyInts; |
| 67 |
fn mul(self, rhs: __RhsT) -> MyInts { |
| 68 |
MyInts(self.0.mul(rhs), self.1.mul(rhs)) |
| 69 |
} |
| 70 |
} |
| 71 |
``` |
| 72 |
|
| 73 |
The behaviour is similar with more or less fields. |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
## Regular structs |
| 79 |
|
| 80 |
When deriving `Mul` for a regular struct with a single field like this: |
| 81 |
|
| 82 |
```rust |
| 83 |
# use derive_more::Mul; |
| 84 |
# |
| 85 |
#[derive(Mul)] |
| 86 |
struct Point1D { |
| 87 |
x: i32, |
| 88 |
} |
| 89 |
``` |
| 90 |
|
| 91 |
Code like this will be generated: |
| 92 |
|
| 93 |
```rust |
| 94 |
# struct Point1D { |
| 95 |
# x: i32, |
| 96 |
# } |
| 97 |
impl<__RhsT> ::core::ops::Mul<__RhsT> for Point1D |
| 98 |
where i32: ::core::ops::Mul<__RhsT, Output = i32> |
| 99 |
{ |
| 100 |
type Output = Point1D; |
| 101 |
fn mul(self, rhs: __RhsT) -> Point1D { |
| 102 |
Point1D { x: self.x.mul(rhs) } |
| 103 |
} |
| 104 |
} |
| 105 |
``` |
| 106 |
|
| 107 |
The behaviour is again slightly different when deriving for a struct with multiple |
| 108 |
fields, because it still needs the `Copy` as well. |
| 109 |
For instance when deriving for a tuple struct with two fields like this: |
| 110 |
|
| 111 |
```rust |
| 112 |
# use derive_more::Mul; |
| 113 |
# |
| 114 |
#[derive(Mul)] |
| 115 |
struct Point2D { |
| 116 |
x: i32, |
| 117 |
y: i32, |
| 118 |
} |
| 119 |
``` |
| 120 |
|
| 121 |
Code like this will be generated: |
| 122 |
|
| 123 |
```rust |
| 124 |
# struct Point2D { |
| 125 |
# x: i32, |
| 126 |
# y: i32, |
| 127 |
# } |
| 128 |
impl<__RhsT: ::core::marker::Copy> ::core::ops::Mul<__RhsT> for Point2D |
| 129 |
where i32: ::core::ops::Mul<__RhsT, Output = i32> |
| 130 |
{ |
| 131 |
type Output = Point2D; |
| 132 |
fn mul(self, rhs: __RhsT) -> Point2D { |
| 133 |
Point2D { |
| 134 |
x: self.x.mul(rhs), |
| 135 |
y: self.y.mul(rhs), |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
``` |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
## Enums |
| 145 |
|
| 146 |
Deriving `Mul` for enums is not (yet) supported, except when you use |
| 147 |
`#[mul(forward)]`. |
| 148 |
Although it shouldn't be impossible no effort has been put into this yet. |