blob: c34e3e571fb1f1145405ae482ed21ef4026e5330 [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),
Myron Ahneba35cf2020-02-05 19:41:51 +070029 Type::RustVec(t) => t.hash(state),
David Tolnay4377a9e2020-04-24 15:20:26 -070030 Type::CxxVector(t) => t.hash(state),
David Tolnay417305a2020-03-18 13:54:00 -070031 Type::Fn(t) => t.hash(state),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070032 Type::Slice(t) => t.hash(state),
33 Type::SliceRefU8(t) => t.hash(state),
David Tolnayc21b20a2020-03-15 23:25:32 -070034 Type::Void(_) => {}
35 }
36 }
37}
38
39impl Eq for Type {}
40
41impl PartialEq for Type {
42 fn eq(&self, other: &Type) -> bool {
43 match (self, other) {
44 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
45 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
46 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
47 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
48 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
Myron Ahneba35cf2020-02-05 19:41:51 +070049 (Type::RustVec(lhs), Type::RustVec(rhs)) => lhs == rhs,
David Tolnay4377a9e2020-04-24 15:20:26 -070050 (Type::CxxVector(lhs), Type::CxxVector(rhs)) => lhs == rhs,
David Tolnay417305a2020-03-18 13:54:00 -070051 (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs,
Adrian Taylorf5dd5522020-04-13 16:50:14 -070052 (Type::Slice(lhs), Type::Slice(rhs)) => lhs == rhs,
53 (Type::SliceRefU8(lhs), Type::SliceRefU8(rhs)) => lhs == rhs,
David Tolnayc21b20a2020-03-15 23:25:32 -070054 (Type::Void(_), Type::Void(_)) => true,
55 (_, _) => false,
56 }
57 }
58}
David Tolnay7db73692019-10-20 14:51:12 -040059
60impl Eq for Ty1 {}
61
62impl PartialEq for Ty1 {
63 fn eq(&self, other: &Ty1) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070064 let Ty1 {
65 name,
66 langle: _,
67 inner,
68 rangle: _,
69 } = self;
70 let Ty1 {
71 name: name2,
72 langle: _,
73 inner: inner2,
74 rangle: _,
75 } = other;
76 name == name2 && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040077 }
78}
79
80impl Hash for Ty1 {
81 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070082 let Ty1 {
83 name,
84 langle: _,
85 inner,
86 rangle: _,
87 } = self;
88 name.hash(state);
89 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -040090 }
91}
92
93impl Eq for Ref {}
94
95impl PartialEq for Ref {
96 fn eq(&self, other: &Ref) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070097 let Ref {
98 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -070099 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -0700100 mutability,
101 inner,
102 } = self;
103 let Ref {
104 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700105 lifetime: lifetime2,
David Tolnay35c82eb2020-03-18 16:48:28 -0700106 mutability: mutability2,
107 inner: inner2,
108 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700109 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -0400110 }
111}
112
113impl Hash for Ref {
114 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -0700115 let Ref {
116 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700117 lifetime,
David Tolnay35c82eb2020-03-18 16:48:28 -0700118 mutability,
119 inner,
120 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700121 lifetime.hash(state);
David Tolnay35c82eb2020-03-18 16:48:28 -0700122 mutability.is_some().hash(state);
123 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400124 }
125}
David Tolnay417305a2020-03-18 13:54:00 -0700126
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700127impl Eq for Slice {}
128
129impl PartialEq for Slice {
130 fn eq(&self, other: &Slice) -> bool {
David Tolnayeb952ba2020-04-14 15:02:24 -0700131 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700132 let Slice {
133 bracket: _,
134 inner: inner2,
135 } = other;
136 inner == inner2
137 }
138}
139
140impl Hash for Slice {
141 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayeb952ba2020-04-14 15:02:24 -0700142 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700143 inner.hash(state);
144 }
145}
146
David Tolnay417305a2020-03-18 13:54:00 -0700147impl Eq for Signature {}
148
149impl PartialEq for Signature {
150 fn eq(&self, other: &Signature) -> bool {
151 let Signature {
152 fn_token: _,
153 receiver,
154 args,
155 ret,
156 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700157 paren_token: _,
158 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700159 } = self;
160 let Signature {
161 fn_token: _,
162 receiver: receiver2,
163 args: args2,
164 ret: ret2,
165 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700166 paren_token: _,
167 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700168 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700169 receiver == receiver2
170 && ret == ret2
171 && throws == throws2
172 && args.len() == args2.len()
173 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700174 }
175}
176
177impl Hash for Signature {
178 fn hash<H: Hasher>(&self, state: &mut H) {
179 let Signature {
180 fn_token: _,
181 receiver,
182 args,
183 ret,
184 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700185 paren_token: _,
186 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700187 } = self;
188 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700189 for arg in args {
190 arg.hash(state);
191 }
David Tolnay417305a2020-03-18 13:54:00 -0700192 ret.hash(state);
193 throws.hash(state);
194 }
195}
196
197impl Eq for Receiver {}
198
199impl PartialEq for Receiver {
200 fn eq(&self, other: &Receiver) -> bool {
David Tolnay417305a2020-03-18 13:54:00 -0700201 let Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700202 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700203 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700204 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700205 var: _,
206 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700207 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700208 } = self;
209 let Receiver {
210 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700211 lifetime: lifetime2,
David Tolnay417305a2020-03-18 13:54:00 -0700212 mutability: mutability2,
David Tolnay05e11cc2020-04-20 02:13:56 -0700213 var: _,
214 ty: ty2,
David Tolnay62d360c2020-04-22 16:26:21 -0700215 shorthand: _,
David Tolnay417305a2020-03-18 13:54:00 -0700216 } = other;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700217 lifetime == lifetime2 && mutability.is_some() == mutability2.is_some() && ty == ty2
David Tolnay417305a2020-03-18 13:54:00 -0700218 }
219}
220
221impl Hash for Receiver {
222 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayfb6e3862020-04-20 01:33:23 -0700223 let Receiver {
224 ampersand: _,
David Tolnay0bd50fa2020-04-22 15:31:33 -0700225 lifetime,
David Tolnayfb6e3862020-04-20 01:33:23 -0700226 mutability,
David Tolnay05e11cc2020-04-20 02:13:56 -0700227 var: _,
228 ty,
David Tolnay62d360c2020-04-22 16:26:21 -0700229 shorthand: _,
David Tolnayfb6e3862020-04-20 01:33:23 -0700230 } = self;
David Tolnay0bd50fa2020-04-22 15:31:33 -0700231 lifetime.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700232 mutability.is_some().hash(state);
David Tolnay05e11cc2020-04-20 02:13:56 -0700233 ty.hash(state);
David Tolnay417305a2020-03-18 13:54:00 -0700234 }
235}