blob: b04075316d5d4d92dcd07f51c775d28973bd6199 [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: _,
89 mutability,
90 inner,
91 } = self;
92 let Ref {
93 ampersand: _,
94 mutability: mutability2,
95 inner: inner2,
96 } = other;
97 mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040098 }
99}
100
101impl Hash for Ref {
102 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -0700103 let Ref {
104 ampersand: _,
105 mutability,
106 inner,
107 } = self;
108 mutability.is_some().hash(state);
109 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400110 }
111}
David Tolnay417305a2020-03-18 13:54:00 -0700112
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700113impl Eq for Slice {}
114
115impl PartialEq for Slice {
116 fn eq(&self, other: &Slice) -> bool {
117 let Slice {
118 bracket: _,
119 inner,
120 } = self;
121 let Slice {
122 bracket: _,
123 inner: inner2,
124 } = other;
125 inner == inner2
126 }
127}
128
129impl Hash for Slice {
130 fn hash<H: Hasher>(&self, state: &mut H) {
131 let Slice {
132 bracket: _,
133 inner,
134 } = self;
135 inner.hash(state);
136 }
137}
138
David Tolnay417305a2020-03-18 13:54:00 -0700139impl Eq for Signature {}
140
141impl PartialEq for Signature {
142 fn eq(&self, other: &Signature) -> bool {
143 let Signature {
144 fn_token: _,
145 receiver,
146 args,
147 ret,
148 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700149 paren_token: _,
150 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700151 } = self;
152 let Signature {
153 fn_token: _,
154 receiver: receiver2,
155 args: args2,
156 ret: ret2,
157 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700158 paren_token: _,
159 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700160 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700161 receiver == receiver2
162 && ret == ret2
163 && throws == throws2
164 && args.len() == args2.len()
165 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700166 }
167}
168
169impl Hash for Signature {
170 fn hash<H: Hasher>(&self, state: &mut H) {
171 let Signature {
172 fn_token: _,
173 receiver,
174 args,
175 ret,
176 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700177 paren_token: _,
178 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700179 } = self;
180 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700181 for arg in args {
182 arg.hash(state);
183 }
David Tolnay417305a2020-03-18 13:54:00 -0700184 ret.hash(state);
185 throws.hash(state);
186 }
187}
188
189impl Eq for Receiver {}
190
191impl PartialEq for Receiver {
192 fn eq(&self, other: &Receiver) -> bool {
193 let Receiver { mutability, ident } = self;
194 let Receiver {
195 mutability: mutability2,
196 ident: ident2,
197 } = other;
198 mutability.is_some() == mutability2.is_some() && ident == ident2
199 }
200}
201
202impl Hash for Receiver {
203 fn hash<H: Hasher>(&self, state: &mut H) {
204 let Receiver { mutability, ident } = self;
205 mutability.is_some().hash(state);
206 ident.hash(state);
207 }
208}