blob: a2ce05b219a6ba5615449538f1f36eb51e751e95 [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 Tolnay9bfbea32020-04-22 18:12:43 -07004use std::ops::{Deref, DerefMut};
David Tolnay16448732020-03-18 12:39:36 -07005
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
David Tolnay9bfbea32020-04-22 18:12:43 -070014impl DerefMut for ExternFn {
15 fn deref_mut(&mut self) -> &mut Self::Target {
16 &mut self.sig
17 }
18}
19
David Tolnayc21b20a2020-03-15 23:25:32 -070020impl Hash for Type {
21 fn hash<H: Hasher>(&self, state: &mut H) {
22 mem::discriminant(self).hash(state);
23 match self {
24 Type::Ident(t) => t.hash(state),
25 Type::RustBox(t) => t.hash(state),
26 Type::UniquePtr(t) => t.hash(state),
27 Type::Ref(t) => t.hash(state),
28 Type::Str(t) => t.hash(state),
David Tolnay417305a2020-03-18 13:54:00 -070029 Type::Fn(t) => t.hash(state),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070030 Type::Slice(t) => t.hash(state),
31 Type::SliceRefU8(t) => t.hash(state),
David Tolnayc21b20a2020-03-15 23:25:32 -070032 Type::Void(_) => {}
33 }
34 }
35}
36
37impl Eq for Type {}
38
39impl PartialEq for Type {
40 fn eq(&self, other: &Type) -> bool {
41 match (self, other) {
42 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
43 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
44 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
45 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
46 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
David Tolnay417305a2020-03-18 13:54:00 -070047 (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs,
Adrian Taylorf5dd5522020-04-13 16:50:14 -070048 (Type::Slice(lhs), Type::Slice(rhs)) => lhs == rhs,
49 (Type::SliceRefU8(lhs), Type::SliceRefU8(rhs)) => lhs == rhs,
David Tolnayc21b20a2020-03-15 23:25:32 -070050 (Type::Void(_), Type::Void(_)) => true,
51 (_, _) => false,
52 }
53 }
54}
David Tolnay7db73692019-10-20 14:51:12 -040055
56impl Eq for Ty1 {}
57
58impl PartialEq for Ty1 {
59 fn eq(&self, other: &Ty1) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070060 let Ty1 {
61 name,
62 langle: _,
63 inner,
64 rangle: _,
65 } = self;
66 let Ty1 {
67 name: name2,
68 langle: _,
69 inner: inner2,
70 rangle: _,
71 } = other;
72 name == name2 && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040073 }
74}
75
76impl Hash for Ty1 {
77 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070078 let Ty1 {
79 name,
80 langle: _,
81 inner,
82 rangle: _,
83 } = self;
84 name.hash(state);
85 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -040086 }
87}
88
89impl Eq for Ref {}
90
91impl PartialEq for Ref {
92 fn eq(&self, other: &Ref) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070093 let Ref {
94 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -070095 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -070096 mutability,
97 inner,
98 } = self;
99 let Ref {
100 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700101 lifetime: lifetime2,
David Tolnay35c82eb2020-03-18 16:48:28 -0700102 mutability: mutability2,
103 inner: inner2,
104 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700105 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -0400106 }
107}
108
109impl Hash for Ref {
110 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -0700111 let Ref {
112 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700113 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -0700114 mutability,
115 inner,
116 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700117 lifetime.hash(state);
David Tolnay35c82eb2020-03-18 16:48:28 -0700118 mutability.is_some().hash(state);
119 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400120 }
121}
David Tolnay417305a2020-03-18 13:54:00 -0700122
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700123impl Eq for Slice {}
124
125impl PartialEq for Slice {
126 fn eq(&self, other: &Slice) -> bool {
David Tolnayeb952ba2020-04-14 15:02:24 -0700127 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700128 let Slice {
129 bracket: _,
130 inner: inner2,
131 } = other;
132 inner == inner2
133 }
134}
135
136impl Hash for Slice {
137 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayeb952ba2020-04-14 15:02:24 -0700138 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700139 inner.hash(state);
140 }
141}
142
David Tolnay417305a2020-03-18 13:54:00 -0700143impl Eq for Signature {}
144
145impl PartialEq for Signature {
146 fn eq(&self, other: &Signature) -> bool {
147 let Signature {
148 fn_token: _,
149 receiver,
150 args,
151 ret,
152 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700153 paren_token: _,
154 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700155 } = self;
156 let Signature {
157 fn_token: _,
158 receiver: receiver2,
159 args: args2,
160 ret: ret2,
161 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700162 paren_token: _,
163 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700164 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700165 receiver == receiver2
166 && ret == ret2
167 && throws == throws2
168 && args.len() == args2.len()
169 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700170 }
171}
172
173impl Hash for Signature {
174 fn hash<H: Hasher>(&self, state: &mut H) {
175 let Signature {
176 fn_token: _,
177 receiver,
178 args,
179 ret,
180 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700181 paren_token: _,
182 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700183 } = self;
184 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700185 for arg in args {
186 arg.hash(state);
187 }
David Tolnay417305a2020-03-18 13:54:00 -0700188 ret.hash(state);
189 throws.hash(state);
190 }
191}
192
193impl Eq for Receiver {}
194
195impl PartialEq for Receiver {
196 fn eq(&self, other: &Receiver) -> bool {
David Tolnay417305a2020-03-18 13:54:00 -0700197 let Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700198 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700199 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700200 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700201 var: _,
202 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700203 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700204 } = self;
205 let Receiver {
206 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700207 lifetime: lifetime2,
David Tolnay417305a2020-03-18 13:54:00 -0700208 mutability: mutability2,
David Tolnay05e11cc2020-04-20 02:13:56 -0700209 var: _,
210 ty: ty2,
David Tolnay62d360c2020-04-22 16:26:21 -0700211 shorthand: _,
David Tolnay417305a2020-03-18 13:54:00 -0700212 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700213 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && ty == ty2
David Tolnay417305a2020-03-18 13:54:00 -0700214 }
215}
216
217impl Hash for Receiver {
218 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayfb6e3862020-04-20 01:33:23 -0700219 let Receiver {
220 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700221 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700222 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700223 var: _,
224 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700225 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700226 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700227 lifetime.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700228 mutability.is_some().hash(state);
David Tolnay05e11cc2020-04-20 02:13:56 -0700229 ty.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700230 }
231}