| 1 | # What `#[derive(AddAssign)]` generates |
| 2 | |
| 3 | > **NOTE**: `SubAssign`, `BitAndAssign`, `BitOrAssign` and `BitXorAssign` derives |
| 4 | > are fully equivalent to the `AddAssign` derive described below. |
| 5 | |
| 6 | Deriving `AddAssign` works very similar to deriving `Add`. The difference is that |
| 7 | it mutates the existing value in-place instead of creating a new one. |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | ## Structs |
| 13 | |
| 14 | The derived `AddAssign` implementation will allow two structs of the same type to |
| 15 | be added together, mutating the first one in-place. This is done by `AddAssign`ing |
| 16 | their respective fields. |
| 17 | |
| 18 | ```rust |
| 19 | # use derive_more::AddAssign; |
| 20 | # |
| 21 | #[derive(AddAssign)] |
| 22 | struct MyInts(i32, i32); |
| 23 | |
| 24 | #[derive(AddAssign)] |
| 25 | struct Point2D { |
| 26 | x: i32, |
| 27 | y: i32, |
| 28 | } |
| 29 | ``` |
| 30 | This generates code equivalent to: |
| 31 | ```rust |
| 32 | # use std::ops::AddAssign; |
| 33 | # |
| 34 | # struct MyInts(i32, i32); |
| 35 | # |
| 36 | # struct Point2D { |
| 37 | # x: i32, |
| 38 | # y: i32, |
| 39 | # } |
| 40 | # |
| 41 | impl AddAssign for MyInts { |
| 42 | fn add_assign(&mut self, rhs: Self) { |
| 43 | match (self, rhs) { |
| 44 | (Self(self_0, self_1), Self(rhs_0, rhs_1)) => { |
| 45 | AddAssign::add_assign(self_0, rhs_0); |
| 46 | AddAssign::add_assign(self_1, rhs_1); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl AddAssign for Point2D { |
| 53 | fn add_assign(&mut self, rhs: Self) { |
| 54 | match (self, rhs) { |
| 55 | (Self { x: self_0, y: self_1 }, |
| 56 | Self { x: rhs_0, y: rhs_1 }) => { |
| 57 | AddAssign::add_assign(self_0, rhs_0); |
| 58 | AddAssign::add_assign(self_1, rhs_1); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | The behaviour is similar with more or less fields. |
| 66 | |
| 67 | |
| 68 | ### Ignoring |
| 69 | |
| 70 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 71 | participate in `AddAssign` implementation. Such field could be ignored using the |
| 72 | `#[add_assign(skip)]` attribute. |
| 73 | |
| 74 | ```rust |
| 75 | # use core::marker::PhantomData; |
| 76 | # use derive_more::AddAssign; |
| 77 | # |
| 78 | #[derive(AddAssign)] |
| 79 | struct TupleWithZst<T>(i32, #[add_assign(skip)] PhantomData<T>); |
| 80 | |
| 81 | #[derive(AddAssign)] |
| 82 | struct StructWithZst<T> { |
| 83 | x: i32, |
| 84 | #[add_assign(skip)] // or #[add_assign(ignore)] |
| 85 | _marker: PhantomData<T>, |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | ## Enums |
| 93 | |
| 94 | Deriving `AddAssign` is not (yet) supported for enums. |
| 95 | |
| 96 | This is mostly due to the fact that it is not trivial convert the `Add` |
| 97 | derivation code, because that returns a `Result<EnumType>` instead of an `EnumType`. |
| 98 | Handling the case where it errors would be hard and maybe impossible. |