| 1 | # Using `#[derive(Eq)]` and `#[derive(PartialEq)]` |
| 2 | |
| 3 | Deriving `Eq`/`PartialEq` works by checking whether two values are equal according |
| 4 | to their type structure. |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | ## Structural equality |
| 10 | |
| 11 | Deriving `Eq`/`PartialEq` for enums/structs works in a similar way to the one in `std`, |
| 12 | by comparing all the available fields, but, in the contrast: |
| 13 | 1. Does not overconstrain generic parameters. |
| 14 | 2. Implements `PartialEq::ne()` method as well, to propagate possible efficient |
| 15 | implementations of this method from the underlying types. |
| 16 | |
| 17 | |
| 18 | ### Structs |
| 19 | |
| 20 | For structs all the available fields are checked for equality. |
| 21 | |
| 22 | ```rust |
| 23 | # use std::marker::PhantomData; |
| 24 | # use derive_more::{Eq, PartialEq}; |
| 25 | # |
| 26 | trait Trait { |
| 27 | type Assoc; |
| 28 | } |
| 29 | impl<T: ?Sized> Trait for T { |
| 30 | type Assoc = u8; |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Eq, PartialEq)] |
| 34 | struct Foo<A, B, C: Trait + ?Sized> { |
| 35 | a: A, |
| 36 | b: PhantomData<B>, |
| 37 | c: C::Assoc, |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug)] |
| 41 | struct NoEq; |
| 42 | |
| 43 | assert_eq!(Foo::<_, NoEq, NoEq> { a: 3, b: PhantomData, c: 0 }, Foo { a: 3, b: PhantomData, c: 0 }); |
| 44 | assert_ne!(Foo::<_, NoEq, NoEq> { a: 3, b: PhantomData, c: 0 }, Foo { a: 0, b: PhantomData, c: 3 }); |
| 45 | ``` |
| 46 | This generates code equivalent to: |
| 47 | ```rust |
| 48 | # use std::marker::PhantomData; |
| 49 | # |
| 50 | # trait Trait { |
| 51 | # type Assoc; |
| 52 | # } |
| 53 | # impl<T: ?Sized> Trait for T { |
| 54 | # type Assoc = u8; |
| 55 | # } |
| 56 | # |
| 57 | # struct Foo<A, B, C: Trait + ?Sized> { |
| 58 | # a: A, |
| 59 | # b: PhantomData<B>, |
| 60 | # c: C::Assoc, |
| 61 | # } |
| 62 | # |
| 63 | impl<A, B, C: Trait + ?Sized> PartialEq for Foo<A, B, C> |
| 64 | where |
| 65 | A: PartialEq, |
| 66 | PhantomData<B>: PartialEq, // `B: PartialEq` is generated by `std` instead |
| 67 | C::Assoc: PartialEq, // `C: PartialEq` is generated by `std` instead |
| 68 | { |
| 69 | fn eq(&self, other: &Self) -> bool { |
| 70 | match (self, other) { |
| 71 | (Self { a: self_0, b: self_1, c: self_2 }, Self { a: other_0, b: other_1, c: other_2 }) => { |
| 72 | self_0 == other_0 && self_1 == other_1 && self_2 == other_2 |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | fn ne(&self, other: &Self) -> bool { |
| 77 | match (self, other) { |
| 78 | (Self { a: self_0, b: self_1, c: self_2 }, Self { a: other_0, b: other_1, c: other_2 }) => { |
| 79 | self_0 != other_0 || self_1 != other_1 || self_2 != other_2 |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl<A, B, C: Trait + ?Sized> Eq for Foo<A, B, C> |
| 86 | where |
| 87 | Self: PartialEq, |
| 88 | A: Eq, |
| 89 | PhantomData<B>: Eq, // `B: Eq` is generated by `std` instead |
| 90 | C::Assoc: Eq, // `C: Eq` is generated by `std` instead |
| 91 | {} |
| 92 | ``` |
| 93 | |
| 94 | |
| 95 | ### Enums |
| 96 | |
| 97 | For enums the first check is whether these two values represent the same variant, |
| 98 | and after that we check fields equality. |
| 99 | |
| 100 | ```rust |
| 101 | # use std::marker::PhantomData; |
| 102 | # use derive_more::{Eq, PartialEq}; |
| 103 | # |
| 104 | # trait Trait { |
| 105 | # type Assoc; |
| 106 | # } |
| 107 | # impl<T: ?Sized> Trait for T { |
| 108 | # type Assoc = u8; |
| 109 | # } |
| 110 | # |
| 111 | #[derive(Debug, Eq, PartialEq)] |
| 112 | enum Foo<A, B, C: Trait + ?Sized> { |
| 113 | A(A), |
| 114 | B { b: PhantomData<B> }, |
| 115 | C(C::Assoc), |
| 116 | } |
| 117 | # |
| 118 | # #[derive(Debug)] |
| 119 | # struct NoEq; |
| 120 | |
| 121 | assert_eq!(Foo::<_, NoEq, NoEq>::A(3), Foo::A(3)); |
| 122 | assert_ne!(Foo::<_, NoEq, NoEq>::A(3), Foo::A(0)); |
| 123 | |
| 124 | assert_eq!(Foo::<u16, NoEq, NoEq>::B { b: PhantomData }, Foo::B { b: PhantomData }); |
| 125 | |
| 126 | assert_eq!(Foo::<i32, NoEq, NoEq>::C(3), Foo::C(3)); |
| 127 | assert_ne!(Foo::<i32, NoEq, NoEq>::C(3), Foo::C(0)); |
| 128 | ``` |
| 129 | This generates code equivalent to: |
| 130 | ```rust |
| 131 | # use std::marker::PhantomData; |
| 132 | # |
| 133 | # trait Trait { |
| 134 | # type Assoc; |
| 135 | # } |
| 136 | # impl<T: ?Sized> Trait for T { |
| 137 | # type Assoc = u8; |
| 138 | # } |
| 139 | # |
| 140 | # enum Foo<A, B, C: Trait + ?Sized> { |
| 141 | # A(A), |
| 142 | # B { b: PhantomData<B> }, |
| 143 | # C(C::Assoc), |
| 144 | # } |
| 145 | # |
| 146 | impl<A, B, C: Trait + ?Sized> PartialEq for Foo<A, B, C> |
| 147 | where |
| 148 | A: PartialEq, |
| 149 | PhantomData<B>: PartialEq, // `B: PartialEq` is generated by `std` instead |
| 150 | C::Assoc: PartialEq, // `C: PartialEq` is generated by `std` instead |
| 151 | { |
| 152 | fn eq(&self, other: &Self) -> bool { |
| 153 | std::mem::discriminant(self) == std::mem::discriminant(other) && |
| 154 | match (self, other) { |
| 155 | (Self::A(self_0), Self::A(other_0)) => { self_0 == other_0 } |
| 156 | (Self::B { b: self_0 }, Self::B { b: other_0 }) => { self_0 == other_0 } |
| 157 | (Self::C(self_0), Self::C(other_0)) => { self_0 == other_0 } |
| 158 | _ => unsafe { std::hint::unreachable_unchecked() } |
| 159 | } |
| 160 | } |
| 161 | fn ne(&self, other: &Self) -> bool { |
| 162 | std::mem::discriminant(self) != std::mem::discriminant(other) || |
| 163 | match (self, other) { |
| 164 | (Self::A(self_0), Self::A(other_0)) => { self_0 != other_0 } |
| 165 | (Self::B { b: self_0 }, Self::B { b: other_0 }) => { self_0 != other_0 } |
| 166 | (Self::C(self_0), Self::C(other_0)) => { self_0 != other_0 } |
| 167 | _ => unsafe { std::hint::unreachable_unchecked() } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl<A, B, C: Trait + ?Sized> Eq for Foo<A, B, C> |
| 173 | where |
| 174 | Self: PartialEq, |
| 175 | A: Eq, |
| 176 | PhantomData<B>: Eq, // `B: Eq` is generated by `std` instead |
| 177 | C::Assoc: Eq, // `C: Eq` is generated by `std` instead |
| 178 | {} |
| 179 | ``` |
| 180 | |
| 181 | |
| 182 | ### Ignoring |
| 183 | |
| 184 | Both `#[eq(skip)]` and `#[partial_eq(skip)]` attributes could be used to ignore |
| 185 | fields, a whole struct or enum variants in the expansion. |
| 186 | |
| 187 | ```rust |
| 188 | # use derive_more::{Eq, PartialEq}; |
| 189 | # |
| 190 | #[derive(Debug)] |
| 191 | struct NoEq; // doesn't implement `Eq`/`PartialEq` |
| 192 | |
| 193 | #[derive(Debug, Eq, PartialEq)] |
| 194 | struct Foo { |
| 195 | num: i32, |
| 196 | // This attribute is seen by both `Eq` and `PartialEq` macros, |
| 197 | // so the repetition is unnecessary. |
| 198 | #[eq(skip)] // or #[eq(ignore)] |
| 199 | ignored: f32, |
| 200 | } |
| 201 | |
| 202 | #[derive(Debug, Eq, PartialEq)] |
| 203 | // This attribute is also seen by both `Eq` and `PartialEq` macros. |
| 204 | // Makes all fields of this struct being ignored. |
| 205 | #[partial_eq(skip)] // or #[partial_eq(ignore)] |
| 206 | struct Bar(f32, NoEq); |
| 207 | |
| 208 | #[derive(Debug, Eq, PartialEq)] |
| 209 | enum Enum { |
| 210 | Foo(i32, #[eq(skip)] NoEq), |
| 211 | #[eq(skip)] |
| 212 | Bar(NoEq), |
| 213 | Baz, |
| 214 | } |
| 215 | |
| 216 | assert_eq!(Foo { num: 0, ignored: 1.0 }, Foo { num: 0, ignored: 1.0 }); |
| 217 | assert_eq!(Foo { num: 0, ignored: 1.0 }, Foo { num: 0, ignored: 2.0 }); |
| 218 | assert_ne!(Foo { num: 0, ignored: 1.0 }, Foo { num: 1, ignored: 1.0 }); |
| 219 | |
| 220 | assert_eq!(Bar(0.0, NoEq), Bar(0.0, NoEq)); |
| 221 | assert_eq!(Bar(0.0, NoEq), Bar(1.0, NoEq)); |
| 222 | |
| 223 | assert_eq!(Enum::Foo(0, NoEq), Enum::Foo(0, NoEq)); |
| 224 | assert_ne!(Enum::Foo(0, NoEq), Enum::Foo(1, NoEq)); |
| 225 | |
| 226 | assert_eq!(Enum::Bar(NoEq), Enum::Bar(NoEq)); |
| 227 | |
| 228 | assert_eq!(Enum::Baz, Enum::Baz); |
| 229 | |
| 230 | // NOTE: Different variants are still not equal despite being ignored! |
| 231 | // Ignoring a variant only ignores all its fields during comparison. |
| 232 | assert_ne!(Enum::Foo(0, NoEq), Enum::Bar(NoEq)); |
| 233 | assert_ne!(Enum::Foo(0, NoEq), Enum::Baz); |
| 234 | assert_ne!(Enum::Bar(NoEq), Enum::Baz); |
| 235 | ``` |
| 236 | This generates code equivalent to: |
| 237 | ```rust |
| 238 | # struct NoEq; |
| 239 | # |
| 240 | # struct Foo { num: i32, ignored: f32 } |
| 241 | # |
| 242 | impl PartialEq for Foo { |
| 243 | fn eq(&self, __other: &Self) -> bool { |
| 244 | match (self, __other) { |
| 245 | (Self { num: __self_0, .. }, Self { num: __other_0, .. }) => { __self_0 == __other_0 } |
| 246 | } |
| 247 | } |
| 248 | fn ne(&self, __other: &Self) -> bool { |
| 249 | match (self, __other) { |
| 250 | (Self { num: __self_0, .. }, Self { num: __other_0, .. }) => { __self_0 != __other_0 } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | impl Eq for Foo |
| 255 | where |
| 256 | i32: Eq, |
| 257 | {} |
| 258 | |
| 259 | # struct Bar(i32, NoEq); |
| 260 | # |
| 261 | impl PartialEq for Bar { |
| 262 | fn eq(&self, __other: &Self) -> bool { true } |
| 263 | } |
| 264 | impl Eq for Bar {} |
| 265 | |
| 266 | # enum Enum { |
| 267 | # Foo(i32, NoEq), |
| 268 | # Bar(NoEq), |
| 269 | # Baz, |
| 270 | # } |
| 271 | # |
| 272 | impl PartialEq for Enum { |
| 273 | fn eq(&self, __other: &Self) -> bool { |
| 274 | std::mem::discriminant(self) == std::mem::discriminant(__other) && |
| 275 | match (self, __other) { |
| 276 | (Self::Foo(__self_0, _, ), Self::Foo(__other_0, _, )) => { __self_0 == __other_0 } |
| 277 | _ => true, |
| 278 | } |
| 279 | } |
| 280 | fn ne(&self, __other: &Self) -> bool { |
| 281 | std::mem::discriminant(self) != std::mem::discriminant(__other) || |
| 282 | match (self, __other) { |
| 283 | (Self::Foo(__self_0, _, ), Self::Foo(__other_0, _, )) => { __self_0 != __other_0 } |
| 284 | _ => false, |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | ``` |