| 1 | # What `#[derive(Not)]` generates |
| 2 | |
| 3 | The derived `Not` implementation simply negates all the fields of a |
| 4 | struct and returns that as a new instance of the struct. |
| 5 | For enums all fields of the active variant of the enum are negated and a new |
| 6 | instance of the same variant with these negated fields is returned. |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | ## Tuple structs |
| 12 | |
| 13 | When deriving for a tuple struct with two fields like this: |
| 14 | |
| 15 | ```rust |
| 16 | # use derive_more::Not; |
| 17 | # |
| 18 | #[derive(Not)] |
| 19 | struct MyInts(i32, i32); |
| 20 | ``` |
| 21 | |
| 22 | Code like this will be generated: |
| 23 | |
| 24 | ```rust |
| 25 | # struct MyInts(i32, i32); |
| 26 | impl derive_more::core::ops::Not for MyInts { |
| 27 | type Output = MyInts; |
| 28 | fn not(self) -> MyInts { |
| 29 | MyInts(self.0.not(), self.1.not()) |
| 30 | } |
| 31 | } |
| 32 | ``` |
| 33 | |
| 34 | The behaviour is similar with more or less fields. |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | ## Regular structs |
| 40 | |
| 41 | When deriving for a regular struct with two fields like this: |
| 42 | |
| 43 | ```rust |
| 44 | # use derive_more::Not; |
| 45 | # |
| 46 | #[derive(Not)] |
| 47 | struct Point2D { |
| 48 | x: i32, |
| 49 | y: i32, |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | Code like this will be generated: |
| 54 | |
| 55 | ```rust |
| 56 | # struct Point2D { |
| 57 | # x: i32, |
| 58 | # y: i32, |
| 59 | # } |
| 60 | impl derive_more::core::ops::Not for Point2D { |
| 61 | type Output = Point2D; |
| 62 | fn not(self) -> Point2D { |
| 63 | Point2D { |
| 64 | x: self.x.not(), |
| 65 | y: self.y.not(), |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | The behaviour is similar with more or less fields. |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | ## Enums |
| 77 | |
| 78 | For each enum variant `Not` is derived in a similar way as it would be derived |
| 79 | if it would be its own type. |
| 80 | For instance when deriving `Not` for an enum like this: |
| 81 | |
| 82 | ```rust |
| 83 | # use derive_more::Not; |
| 84 | # |
| 85 | #[derive(Not)] |
| 86 | enum MixedInts { |
| 87 | SmallInt(i32), |
| 88 | BigInt(i64), |
| 89 | TwoSmallInts(i32, i32), |
| 90 | NamedSmallInts { x: i32, y: i32 }, |
| 91 | UnsignedOne(u32), |
| 92 | UnsignedTwo(u32), |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | Code like this will be generated: |
| 97 | |
| 98 | ```rust |
| 99 | # enum MixedInts { |
| 100 | # SmallInt(i32), |
| 101 | # BigInt(i64), |
| 102 | # TwoSmallInts(i32, i32), |
| 103 | # NamedSmallInts { x: i32, y: i32 }, |
| 104 | # UnsignedOne(u32), |
| 105 | # UnsignedTwo(u32), |
| 106 | # } |
| 107 | impl derive_more::core::ops::Not for MixedInts { |
| 108 | type Output = MixedInts; |
| 109 | fn not(self) -> MixedInts { |
| 110 | match self { |
| 111 | MixedInts::SmallInt(__0) => MixedInts::SmallInt(__0.not()), |
| 112 | MixedInts::BigInt(__0) => MixedInts::BigInt(__0.not()), |
| 113 | MixedInts::TwoSmallInts(__0, __1) => MixedInts::TwoSmallInts(__0.not(), __1.not()), |
| 114 | MixedInts::NamedSmallInts { x: __0, y: __1 } => { |
| 115 | MixedInts::NamedSmallInts { |
| 116 | x: __0.not(), |
| 117 | y: __1.not(), |
| 118 | } |
| 119 | } |
| 120 | MixedInts::UnsignedOne(__0) => MixedInts::UnsignedOne(__0.not()), |
| 121 | MixedInts::UnsignedTwo(__0) => MixedInts::UnsignedTwo(__0.not()), |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | There is one important thing to remember though. |
| 128 | If you add a unit variant to the enum its return type will change from |
| 129 | `EnumType` to `Result<EnumType>`. |
| 130 | This is because Unit cannot have `Not` implemented. |
| 131 | So, when deriving `Not` for an enum like this: |
| 132 | |
| 133 | ```rust |
| 134 | # use derive_more::Not; |
| 135 | # |
| 136 | #[derive(Not)] |
| 137 | enum EnumWithUnit { |
| 138 | SmallInt(i32), |
| 139 | Unit, |
| 140 | } |
| 141 | ``` |
| 142 | |
| 143 | Code like this will be generated: |
| 144 | |
| 145 | ```rust |
| 146 | # enum EnumWithUnit { |
| 147 | # SmallInt(i32), |
| 148 | # Unit, |
| 149 | # } |
| 150 | impl derive_more::core::ops::Not for EnumWithUnit { |
| 151 | type Output = Result<EnumWithUnit, derive_more::UnitError>; |
| 152 | fn not(self) -> Result<EnumWithUnit, derive_more::UnitError> { |
| 153 | match self { |
| 154 | EnumWithUnit::SmallInt(__0) => Ok(EnumWithUnit::SmallInt(__0.not())), |
| 155 | EnumWithUnit::Unit => Err(derive_more::UnitError::new("not")), |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | ``` |