blob: acc99abde2bd72cce76a8d8f5ac8de5cd5cda70b [file] [log] [blame]
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001use crate::syntax::{ExternFn, Receiver, Ref, Signature, Slice, Ty1, Type};
David Tolnay7db73692019-10-20 14:51:12 -04002use std::hash::{Hash, Hasher};
David Tolnayc21b20a2020-03-15 23:25:32 -07003use std::mem;
David Tolnay16448732020-03-18 12:39:36 -07004use std::ops::Deref;
5
6impl Deref for ExternFn {
7 type Target = Signature;
8
9 fn deref(&self) -> &Self::Target {
10 &self.sig
11 }
12}
David Tolnayc21b20a2020-03-15 23:25:32 -070013
14impl Hash for Type {
15 fn hash<H: Hasher>(&self, state: &mut H) {
16 mem::discriminant(self).hash(state);
17 match self {
18 Type::Ident(t) => t.hash(state),
19 Type::RustBox(t) => t.hash(state),
20 Type::UniquePtr(t) => t.hash(state),
21 Type::Ref(t) => t.hash(state),
22 Type::Str(t) => t.hash(state),
David Tolnay417305a2020-03-18 13:54:00 -070023 Type::Fn(t) => t.hash(state),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070024 Type::Slice(t) => t.hash(state),
25 Type::SliceRefU8(t) => t.hash(state),
David Tolnayc21b20a2020-03-15 23:25:32 -070026 Type::Void(_) => {}
27 }
28 }
29}
30
31impl Eq for Type {}
32
33impl PartialEq for Type {
34 fn eq(&self, other: &Type) -> bool {
35 match (self, other) {
36 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
37 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
38 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
39 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
40 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
David Tolnay417305a2020-03-18 13:54:00 -070041 (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs,
Adrian Taylorf5dd5522020-04-13 16:50:14 -070042 (Type::Slice(lhs), Type::Slice(rhs)) => lhs == rhs,
43 (Type::SliceRefU8(lhs), Type::SliceRefU8(rhs)) => lhs == rhs,
David Tolnayc21b20a2020-03-15 23:25:32 -070044 (Type::Void(_), Type::Void(_)) => true,
45 (_, _) => false,
46 }
47 }
48}
David Tolnay7db73692019-10-20 14:51:12 -040049
50impl Eq for Ty1 {}
51
52impl PartialEq for Ty1 {
53 fn eq(&self, other: &Ty1) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070054 let Ty1 {
55 name,
56 langle: _,
57 inner,
58 rangle: _,
59 } = self;
60 let Ty1 {
61 name: name2,
62 langle: _,
63 inner: inner2,
64 rangle: _,
65 } = other;
66 name == name2 && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68}
69
70impl Hash for Ty1 {
71 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070072 let Ty1 {
73 name,
74 langle: _,
75 inner,
76 rangle: _,
77 } = self;
78 name.hash(state);
79 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -040080 }
81}
82
83impl Eq for Ref {}
84
85impl PartialEq for Ref {
86 fn eq(&self, other: &Ref) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070087 let Ref {
88 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -070089 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -070090 mutability,
91 inner,
92 } = self;
93 let Ref {
94 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -070095 lifetime: lifetime2,
David Tolnay35c82eb2020-03-18 16:48:28 -070096 mutability: mutability2,
97 inner: inner2,
98 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -070099 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -0400100 }
101}
102
103impl Hash for Ref {
104 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -0700105 let Ref {
106 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700107 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -0700108 mutability,
109 inner,
110 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700111 lifetime.hash(state);
David Tolnay35c82eb2020-03-18 16:48:28 -0700112 mutability.is_some().hash(state);
113 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400114 }
115}
David Tolnay417305a2020-03-18 13:54:00 -0700116
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700117impl Eq for Slice {}
118
119impl PartialEq for Slice {
120 fn eq(&self, other: &Slice) -> bool {
David Tolnayeb952ba2020-04-14 15:02:24 -0700121 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700122 let Slice {
123 bracket: _,
124 inner: inner2,
125 } = other;
126 inner == inner2
127 }
128}
129
130impl Hash for Slice {
131 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayeb952ba2020-04-14 15:02:24 -0700132 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700133 inner.hash(state);
134 }
135}
136
David Tolnay417305a2020-03-18 13:54:00 -0700137impl Eq for Signature {}
138
139impl PartialEq for Signature {
140 fn eq(&self, other: &Signature) -> bool {
141 let Signature {
142 fn_token: _,
143 receiver,
144 args,
145 ret,
146 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700147 paren_token: _,
148 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700149 } = self;
150 let Signature {
151 fn_token: _,
152 receiver: receiver2,
153 args: args2,
154 ret: ret2,
155 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700156 paren_token: _,
157 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700158 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700159 receiver == receiver2
160 && ret == ret2
161 && throws == throws2
162 && args.len() == args2.len()
163 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700164 }
165}
166
167impl Hash for Signature {
168 fn hash<H: Hasher>(&self, state: &mut H) {
169 let Signature {
170 fn_token: _,
171 receiver,
172 args,
173 ret,
174 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700175 paren_token: _,
176 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700177 } = self;
178 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700179 for arg in args {
180 arg.hash(state);
181 }
David Tolnay417305a2020-03-18 13:54:00 -0700182 ret.hash(state);
183 throws.hash(state);
184 }
185}
186
187impl Eq for Receiver {}
188
189impl PartialEq for Receiver {
190 fn eq(&self, other: &Receiver) -> bool {
David Tolnay417305a2020-03-18 13:54:00 -0700191 let Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700192 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700193 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700194 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700195 var: _,
196 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700197 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700198 } = self;
199 let Receiver {
200 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700201 lifetime: lifetime2,
David Tolnay417305a2020-03-18 13:54:00 -0700202 mutability: mutability2,
David Tolnay05e11cc2020-04-20 02:13:56 -0700203 var: _,
204 ty: ty2,
David Tolnay62d360c2020-04-22 16:26:21 -0700205 shorthand: _,
David Tolnay417305a2020-03-18 13:54:00 -0700206 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700207 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && ty == ty2
David Tolnay417305a2020-03-18 13:54:00 -0700208 }
209}
210
211impl Hash for Receiver {
212 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayfb6e3862020-04-20 01:33:23 -0700213 let Receiver {
214 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700215 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700216 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700217 var: _,
218 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700219 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700220 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700221 lifetime.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700222 mutability.is_some().hash(state);
David Tolnay05e11cc2020-04-20 02:13:56 -0700223 ty.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700224 }
225}