| 1 | # What `#[derive(Add)]` generates |
| 2 | |
| 3 | > **NOTE**: `Sub`, `BitAnd`, `BitOr` and `BitXor` derives are fully equivalent |
| 4 | > to the `Add` derive described below. |
| 5 | |
| 6 | Deriving `Add` works by adding two values structurally (field by field, according |
| 7 | to their type structure). |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | ## Structs |
| 13 | |
| 14 | The derived `Add` implementation will allow two structs of the same type to be |
| 15 | added together. This is done by `Add`ing their respective fields together and |
| 16 | creating a new struct with those values. |
| 17 | |
| 18 | ```rust |
| 19 | # use derive_more::Add; |
| 20 | # |
| 21 | #[derive(Add)] |
| 22 | struct MyInts(i32, i32); |
| 23 | |
| 24 | #[derive(Add)] |
| 25 | struct Point2D { |
| 26 | x: i32, |
| 27 | y: i32, |
| 28 | } |
| 29 | ``` |
| 30 | This generates code equivalent to: |
| 31 | ```rust |
| 32 | # use std::ops::Add; |
| 33 | # |
| 34 | # struct MyInts(i32, i32); |
| 35 | # |
| 36 | # struct Point2D { |
| 37 | # x: i32, |
| 38 | # y: i32, |
| 39 | # } |
| 40 | # |
| 41 | impl Add for MyInts { |
| 42 | type Output = Self; |
| 43 | |
| 44 | fn add(self, rhs: Self) -> Self::Output { |
| 45 | match (self, rhs) { |
| 46 | (Self(self_0, self_1), Self(rhs_0, rhs_1)) => { |
| 47 | Self(Add::add(self_0, rhs_0), Add::add(self_1, rhs_1)) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Add for Point2D { |
| 54 | type Output = Self; |
| 55 | |
| 56 | fn add(self, rhs: Self) -> Self::Output { |
| 57 | match (self, rhs) { |
| 58 | (Self { x: self_0, y: self_1 }, Self { x: rhs_0, y: rhs_1 }) => { |
| 59 | Self { x: Add::add(self_0, rhs_0), y: Add::add(self_1, rhs_1) } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | The behaviour is similar with more or less fields. |
| 67 | |
| 68 | |
| 69 | ### Ignoring |
| 70 | |
| 71 | Sometimes a struct needs to hold a field (most commonly `PhantomData`) that doesn't |
| 72 | participate in `Add` implementation. Such field could be ignored using the `#[add(skip)]` |
| 73 | attribute. |
| 74 | |
| 75 | ```rust |
| 76 | # use core::marker::PhantomData; |
| 77 | # use derive_more::Add; |
| 78 | # |
| 79 | #[derive(Add)] |
| 80 | struct TupleWithZst<T>(i32, #[add(skip)] PhantomData<T>); |
| 81 | |
| 82 | #[derive(Add)] |
| 83 | struct StructWithZst<T> { |
| 84 | x: i32, |
| 85 | #[add(skip)] // or #[add(ignore)] |
| 86 | _marker: PhantomData<T>, |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | ## Enums |
| 94 | |
| 95 | For enums each variant can be `Add`ed in a similar way to another instance of the |
| 96 | same variant. There's one big difference however: it returns a `Result<EnumType>`, |
| 97 | because an error is returned when two different variants are `Add`ed together. |
| 98 | |
| 99 | ```rust |
| 100 | # use derive_more::Add; |
| 101 | # |
| 102 | #[derive(Add)] |
| 103 | enum MixedInts { |
| 104 | SmallInt(i32), |
| 105 | BigInt(i64), |
| 106 | TwoSmallInts(i32, i32), |
| 107 | NamedSmallInts { x: i32, y: i32 }, |
| 108 | UnsignedOne(u32), |
| 109 | UnsignedTwo(u32), |
| 110 | Unit, |
| 111 | } |
| 112 | ``` |
| 113 | This generates code equivalent to: |
| 114 | ```rust |
| 115 | # use std::ops::Add; |
| 116 | # |
| 117 | # enum MixedInts { |
| 118 | # SmallInt(i32), |
| 119 | # BigInt(i64), |
| 120 | # TwoSmallInts(i32, i32), |
| 121 | # NamedSmallInts { x: i32, y: i32 }, |
| 122 | # UnsignedOne(u32), |
| 123 | # UnsignedTwo(u32), |
| 124 | # Unit, |
| 125 | # } |
| 126 | # |
| 127 | impl Add for MixedInts { |
| 128 | type Output = Result<Self, derive_more::BinaryError>; |
| 129 | |
| 130 | fn add(self, rhs: Self) -> Self::Output { |
| 131 | match (self, rhs) { |
| 132 | (Self::SmallInt(self_0), Self::SmallInt(rhs_0)) => { |
| 133 | Ok(Self::SmallInt(Add::add(self_0, rhs_0))) |
| 134 | } |
| 135 | (Self::BigInt(self_0), Self::BigInt(rhs_0)) => { |
| 136 | Ok(Self::BigInt(Add::add(self_0, rhs_0))) |
| 137 | } |
| 138 | (Self::TwoSmallInts(self_0, self_1), Self::TwoSmallInts(rhs_0, rhs_1)) => { |
| 139 | Ok(Self::TwoSmallInts(Add::add(self_0, rhs_0), Add::add(self_1, rhs_1))) |
| 140 | } |
| 141 | (Self::NamedSmallInts { x: self_0, y: self_1 }, |
| 142 | Self::NamedSmallInts { x: rhs_0, y: rhs_1 }) => { |
| 143 | Ok(Self::NamedSmallInts { |
| 144 | x: Add::add(self_0, rhs_0), |
| 145 | y: Add::add(self_1, rhs_1), |
| 146 | }) |
| 147 | } |
| 148 | (Self::UnsignedOne(self_0), Self::UnsignedOne(rhs_0)) => { |
| 149 | Ok(Self::UnsignedOne(Add::add(self_0, rhs_0))) |
| 150 | } |
| 151 | (Self::UnsignedTwo(self_0), Self::UnsignedTwo(rhs_0)) => { |
| 152 | Ok(Self::UnsignedTwo(Add::add(self_0, rhs_0))) |
| 153 | } |
| 154 | (Self::Unit, Self::Unit) => Err(derive_more::BinaryError::Unit( |
| 155 | derive_more::UnitError::new("add"), |
| 156 | )), |
| 157 | _ => Err(derive_more::BinaryError::Mismatch( |
| 158 | derive_more::WrongVariantError::new("add"), |
| 159 | )), |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | ``` |
| 164 | |
| 165 | Also note the `Unit` variant that throws a `derive_more::UnitError` when `Add`ing it to itself. |
| 166 | |
| 167 | |
| 168 | ### Ignoring |
| 169 | |
| 170 | Similarly to structs, enum fields could be ignored using the `#[add(skip)]` attribute. |
| 171 | |
| 172 | ```rust |
| 173 | # use derive_more::Add; |
| 174 | # |
| 175 | struct I32(i32); // doesn't implement `Add` |
| 176 | |
| 177 | #[derive(Add)] |
| 178 | enum MixedInts { |
| 179 | TwoSmallInts(i32, #[add(skip)] I32), |
| 180 | NamedSmallInts { |
| 181 | #[add(skip)] // or #[add(ignore)] |
| 182 | x: I32, |
| 183 | y: i32, |
| 184 | }, |
| 185 | } |
| 186 | ``` |
| 187 | |
| 188 | > **NOTE**: Ignoring all the fields of a variant or ignoring the variant itself is not allowed |
| 189 | > (results in a compilation error). |