|
| 1 | +# Using `#[derive(PartialEq)]` |
| 2 | + |
| 3 | +Deriving `PartialEq` works by checking whether two values are equal according to |
| 4 | +their type structure. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Structural equality |
| 10 | + |
| 11 | +Deriving `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, does not overconstrain |
| 13 | +generic parameters. |
| 14 | + |
| 15 | + |
| 16 | +### Structs |
| 17 | + |
| 18 | +For structs all the available fields are checked for equality. |
| 19 | + |
| 20 | +```rust |
| 21 | +# use std::marker::PhantomData; |
| 22 | +# use derive_more::PartialEq; |
| 23 | +# |
| 24 | +trait Trait { |
| 25 | + type Assoc; |
| 26 | +} |
| 27 | +impl<T: ?Sized> Trait for T { |
| 28 | + type Assoc = u8; |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, PartialEq)] |
| 32 | +struct Foo<A, B, C: Trait + ?Sized> { |
| 33 | + a: A, |
| 34 | + b: PhantomData<B>, |
| 35 | + c: C::Assoc, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug)] |
| 39 | +struct NoEq; |
| 40 | + |
| 41 | +assert_eq!(Foo::<_, NoEq, NoEq> { a: 3, b: PhantomData, c: 0 }, Foo { a: 3, b: PhantomData, c: 0 }); |
| 42 | +assert_ne!(Foo::<_, NoEq, NoEq> { a: 3, b: PhantomData, c: 0 }, Foo { a: 0, b: PhantomData, c: 3 }); |
| 43 | +``` |
| 44 | +This generates code equivalent to: |
| 45 | +```rust |
| 46 | +# use std::marker::PhantomData; |
| 47 | +# |
| 48 | +# trait Trait { |
| 49 | +# type Assoc; |
| 50 | +# } |
| 51 | +# impl<T: ?Sized> Trait for T { |
| 52 | +# type Assoc = u8; |
| 53 | +# } |
| 54 | +# |
| 55 | +# struct Foo<A, B, C: Trait + ?Sized> { |
| 56 | +# a: A, |
| 57 | +# b: PhantomData<B>, |
| 58 | +# c: C::Assoc, |
| 59 | +# } |
| 60 | +# |
| 61 | +impl<A, B, C: Trait + ?Sized> PartialEq for Foo<A, B, C> |
| 62 | +where |
| 63 | + A: PartialEq, |
| 64 | + PhantomData<B>: PartialEq, // `B: PartialEq` is generated by `std` instead |
| 65 | + C::Assoc: PartialEq, // `C: PartialEq` is generated by `std` instead |
| 66 | +{ |
| 67 | + #[inline] |
| 68 | + fn eq(&self, other: &Self) -> bool { |
| 69 | + match (self, other) { |
| 70 | + (Self { a: self_0, b: self_1, c: self_2 }, Self { a: other_0, b: other_1, c: other_2 }) => { |
| 71 | + self_0 == other_0 && self_1 == other_1 && self_2 == other_2 |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +### Enums |
| 80 | + |
| 81 | +For enums the first check is whether these two values represent the same variant, |
| 82 | +and after that we check fields equality. |
| 83 | + |
| 84 | +```rust |
| 85 | +# use std::marker::PhantomData; |
| 86 | +# use derive_more::PartialEq; |
| 87 | +# |
| 88 | +# trait Trait { |
| 89 | +# type Assoc; |
| 90 | +# } |
| 91 | +# impl<T: ?Sized> Trait for T { |
| 92 | +# type Assoc = u8; |
| 93 | +# } |
| 94 | +# |
| 95 | +#[derive(Debug, PartialEq)] |
| 96 | +enum Foo<A, B, C: Trait + ?Sized> { |
| 97 | + A(A), |
| 98 | + B { b: PhantomData<B> }, |
| 99 | + C(C::Assoc), |
| 100 | +} |
| 101 | +# |
| 102 | +# #[derive(Debug)] |
| 103 | +# struct NoEq; |
| 104 | + |
| 105 | +assert_eq!(Foo::<_, NoEq, NoEq>::A(3), Foo::A(3)); |
| 106 | +assert_ne!(Foo::<_, NoEq, NoEq>::A(3), Foo::A(0)); |
| 107 | + |
| 108 | +assert_eq!(Foo::<u16, NoEq, NoEq>::B { b: PhantomData }, Foo::B { b: PhantomData }); |
| 109 | + |
| 110 | +assert_eq!(Foo::<i32, NoEq, NoEq>::C(3), Foo::C(3)); |
| 111 | +assert_ne!(Foo::<i32, NoEq, NoEq>::C(3), Foo::C(0)); |
| 112 | +``` |
| 113 | +This generates code equivalent to: |
| 114 | +```rust |
| 115 | +# use std::marker::PhantomData; |
| 116 | +# |
| 117 | +# trait Trait { |
| 118 | +# type Assoc; |
| 119 | +# } |
| 120 | +# impl<T: ?Sized> Trait for T { |
| 121 | +# type Assoc = u8; |
| 122 | +# } |
| 123 | +# |
| 124 | +# enum Foo<A, B, C: Trait + ?Sized> { |
| 125 | +# A(A), |
| 126 | +# B { b: PhantomData<B> }, |
| 127 | +# C(C::Assoc), |
| 128 | +# } |
| 129 | +# |
| 130 | +impl<A, B, C: Trait + ?Sized> PartialEq for Foo<A, B, C> |
| 131 | +where |
| 132 | + A: PartialEq, |
| 133 | + PhantomData<B>: PartialEq, // `B: PartialEq` is generated by `std` instead |
| 134 | + C::Assoc: PartialEq, // `C: PartialEq` is generated by `std` instead |
| 135 | +{ |
| 136 | + #[inline] |
| 137 | + fn eq(&self, other: &Self) -> bool { |
| 138 | + std::mem::discriminant(self) == std::mem::discriminant(other) && |
| 139 | + match (self, other) { |
| 140 | + (Self::A(self_0), Self::A(other_0)) => { self_0 == other_0 } |
| 141 | + (Self::B { b: self_0 }, Self::B { b: other_0 }) => { self_0 == other_0 } |
| 142 | + (Self::C(self_0), Self::C(other_0)) => { self_0 == other_0 } |
| 143 | + _ => unsafe { std::hint::unreachable_unchecked() }, |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
0 commit comments