1 # What `#[derive(Add)]` generates
3 The derived `Add` implementation will allow two structs from the same type to be
4 added together. This done by adding their respective fields together and
5 creating a new struct with those values.
6 For enums each variant can be added in a similar way to another instance of that
7 same variant. There's one big difference however, it returns a
8 `Result<EnumType>`, because an error is returned when to different variants are
16 When deriving `Add` for a tuple struct with two fields like this:
19 # use derive_more::Add;
22 struct MyInts(i32, i32);
25 Code like this will be generated:
28 # struct MyInts(i32, i32);
29 impl ::core::ops::Add for MyInts {
31 fn add(self, rhs: MyInts) -> MyInts {
32 MyInts(self.0.add(rhs.0), self.1.add(rhs.1))
37 The behaviour is similar with more or less fields.
44 When deriving `Add` for a regular struct with two fields like this:
47 # use derive_more::Add;
56 Code like this will be generated:
63 impl ::core::ops::Add for Point2D {
64 type Output = Point2D;
65 fn add(self, rhs: Point2D) -> Point2D {
74 The behaviour is similar for more or less fields.
81 There's a big difference between the code that is generated for the two struct
82 types and the one that is generated for enums. The code for enums returns
83 `Result<EnumType>` instead of an `EnumType` itself. This is because adding an
84 enum to another enum is only possible if both are the same variant. This makes
85 the generated code much more complex as well, because this check needs to be
86 done. For instance when deriving `Add` for an enum like this:
89 # use derive_more::Add;
95 TwoSmallInts(i32, i32),
96 NamedSmallInts { x: i32, y: i32 },
103 Code like this will be generated:
109 # TwoSmallInts(i32, i32),
110 # NamedSmallInts { x: i32, y: i32 },
115 impl ::core::ops::Add for MixedInts {
116 type Output = Result<MixedInts, ::derive_more::ops::BinaryError>;
117 fn add(self, rhs: MixedInts) -> Result<MixedInts, ::derive_more::ops::BinaryError> {
119 (MixedInts::SmallInt(__l_0), MixedInts::SmallInt(__r_0)) => {
120 Ok(MixedInts::SmallInt(__l_0.add(__r_0)))
122 (MixedInts::BigInt(__l_0), MixedInts::BigInt(__r_0)) => {
123 Ok(MixedInts::BigInt(__l_0.add(__r_0)))
125 (MixedInts::TwoSmallInts(__l_0, __l_1), MixedInts::TwoSmallInts(__r_0, __r_1)) => {
126 Ok(MixedInts::TwoSmallInts(__l_0.add(__r_0), __l_1.add(__r_1)))
128 (MixedInts::NamedSmallInts { x: __l_0, y: __l_1 },
129 MixedInts::NamedSmallInts { x: __r_0, y: __r_1 }) => {
130 Ok(MixedInts::NamedSmallInts {
135 (MixedInts::UnsignedOne(__l_0), MixedInts::UnsignedOne(__r_0)) => {
136 Ok(MixedInts::UnsignedOne(__l_0.add(__r_0)))
138 (MixedInts::UnsignedTwo(__l_0), MixedInts::UnsignedTwo(__r_0)) => {
139 Ok(MixedInts::UnsignedTwo(__l_0.add(__r_0)))
141 (MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::ops::BinaryError::Unit(
142 ::derive_more::ops::UnitError::new("add"),
144 _ => Err(::derive_more::ops::BinaryError::Mismatch(
145 ::derive_more::ops::WrongVariantError::new("add"),
152 Also note the Unit type that throws an error when adding it to itself.