| 1 |
# What `#[derive(AddAssign)]` generates |
| 2 |
|
| 3 |
This code is very similar to the code that is generated for `#[derive(Add)]`. |
| 4 |
The difference is that it mutates the existing instance instead of creating a |
| 5 |
new one. |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
## Tuple structs |
| 11 |
|
| 12 |
When deriving `AddAssign` for a tuple struct with two fields like this: |
| 13 |
|
| 14 |
```rust |
| 15 |
# use derive_more::AddAssign; |
| 16 |
# |
| 17 |
#[derive(AddAssign)] |
| 18 |
struct MyInts(i32, i32); |
| 19 |
``` |
| 20 |
|
| 21 |
Code like this will be generated: |
| 22 |
|
| 23 |
```rust |
| 24 |
# struct MyInts(i32, i32); |
| 25 |
impl ::core::ops::AddAssign for MyInts { |
| 26 |
fn add_assign(&mut self, rhs: MyInts) { |
| 27 |
self.0.add_assign(rhs.0); |
| 28 |
self.1.add_assign(rhs.1); |
| 29 |
} |
| 30 |
} |
| 31 |
``` |
| 32 |
|
| 33 |
The behaviour is similar with more or less fields. |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
## Regular structs |
| 39 |
|
| 40 |
When deriving for a regular struct with two fields like this: |
| 41 |
|
| 42 |
```rust |
| 43 |
# use derive_more::AddAssign; |
| 44 |
# |
| 45 |
#[derive(AddAssign)] |
| 46 |
struct Point2D { |
| 47 |
x: i32, |
| 48 |
y: i32, |
| 49 |
} |
| 50 |
``` |
| 51 |
|
| 52 |
Code like this will be generated: |
| 53 |
|
| 54 |
```rust |
| 55 |
# struct Point2D { |
| 56 |
# x: i32, |
| 57 |
# y: i32, |
| 58 |
# } |
| 59 |
impl ::core::ops::AddAssign for Point2D { |
| 60 |
fn add_assign(&mut self, rhs: Point2D) { |
| 61 |
self.x.add_assign(rhs.x); |
| 62 |
self.y.add_assign(rhs.y); |
| 63 |
} |
| 64 |
} |
| 65 |
``` |
| 66 |
|
| 67 |
The behaviour is similar with more or less fields. |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
## Enums |
| 73 |
|
| 74 |
Deriving `AddAssign` is not (yet) supported for enums. |
| 75 |
This is mostly due to the fact that it is not trivial convert the `Add` |
| 76 |
derivation code, because that returns a `Result<EnumType>` instead of an |
| 77 |
`EnumType`. |
| 78 |
Handling the case where it errors would be hard and maybe impossible. |