1 #![allow(clippy::eq_op, clippy::trivially_copy_pass_by_ref)]
2 
3 #[cfg(feature = "use_core")]
4 extern crate core;
5 
6 #[macro_use]
7 extern crate derivative;
8 
9 #[derive(Derivative)]
10 #[derivative(PartialEq)]
11 #[repr(C, packed)]
12 struct Foo {
13     foo: u8,
14 }
15 
16 #[derive(Derivative)]
17 #[derivative(PartialEq)]
18 #[repr(C, packed)]
19 struct WithPtr<T: ?Sized> {
20     #[derivative(PartialEq(bound = ""))]
21     foo: *const T,
22 }
23 
24 #[derive(Derivative)]
25 #[derivative(PartialEq)]
26 #[repr(C, packed)]
27 struct Empty;
28 
29 #[derive(Derivative)]
30 #[derivative(PartialEq)]
31 #[repr(C, packed)]
32 struct AllIgnored {
33     #[derivative(PartialEq = "ignore")]
34     foo: u8,
35 }
36 
37 #[derive(Derivative)]
38 #[derivative(PartialEq)]
39 #[repr(C, packed)]
40 struct OneIgnored {
41     #[derivative(PartialEq = "ignore")]
42     foo: u8,
43     bar: u8,
44 }
45 
46 #[derive(Derivative)]
47 #[derivative(PartialEq)]
48 #[repr(C, packed)]
49 struct Parity(#[derivative(PartialEq(compare_with = "same_parity"))] u8);
50 
same_parity(lhs: &u8, rhs: &u8) -> bool51 fn same_parity(lhs: &u8, rhs: &u8) -> bool {
52     lhs % 2 == rhs % 2
53 }
54 
55 #[derive(Derivative)]
56 #[derivative(PartialEq)]
57 #[repr(C, packed)]
58 struct Generic<T>(#[derivative(PartialEq(compare_with = "dummy_cmp", bound = ""))] T);
59 
dummy_cmp<T>(_: &T, _: &T) -> bool60 fn dummy_cmp<T>(_: &T, _: &T) -> bool {
61     true
62 }
63 
64 struct NonPartialEq;
65 
66 #[derive(Derivative)]
67 #[derivative(PartialEq, Eq)]
68 #[repr(C, packed)]
69 struct GenericIgnore<T> {
70     f: u32,
71     #[derivative(PartialEq = "ignore")]
72     t: T,
73 }
74 
75 trait SomeTrait {}
76 
77 #[derive(Copy, Clone)]
78 struct SomeType {
79     #[allow(dead_code)]
80     foo: u8,
81 }
82 impl SomeTrait for SomeType {}
83 
84 #[test]
main()85 fn main() {
86     assert!(Foo { foo: 7 } == Foo { foo: 7 });
87     assert!(Foo { foo: 7 } != Foo { foo: 42 });
88 
89     let ptr1: *const dyn SomeTrait = &SomeType { foo: 0 };
90     let ptr2: *const dyn SomeTrait = &SomeType { foo: 1 };
91     assert!(WithPtr { foo: ptr1 } == WithPtr { foo: ptr1 });
92     assert!(WithPtr { foo: ptr1 } != WithPtr { foo: ptr2 });
93 
94     assert!(Empty == Empty);
95     assert!(AllIgnored { foo: 0 } == AllIgnored { foo: 42 });
96     assert!(OneIgnored { foo: 0, bar: 6 } == OneIgnored { foo: 42, bar: 6 });
97     assert!(OneIgnored { foo: 0, bar: 6 } != OneIgnored { foo: 42, bar: 7 });
98 
99     assert!(Option::Some(42) == Option::Some(42));
100     assert!(Option::Some(0) != Option::Some(42));
101     assert!(Option::Some(42) != Option::None);
102     assert!(Option::None != Option::Some(42));
103     assert!(Option::None::<u8> == Option::None::<u8>);
104 
105     assert!(Parity(3) == Parity(7));
106     assert!(Parity(2) == Parity(42));
107     assert!(Parity(3) != Parity(42));
108     assert!(Parity(2) != Parity(7));
109 
110     assert!(Generic(SomeType { foo: 0 }) == Generic(SomeType { foo: 0 }));
111     assert!(
112         GenericIgnore {
113             f: 123,
114             t: NonPartialEq
115         } == GenericIgnore {
116             f: 123,
117             t: NonPartialEq
118         }
119     );
120 }
121