blob: d3c4b0fa1056831e2d04b894694e541a7098b3e4 [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 {
David Tolnayeb952ba2020-04-14 15:02:24 -0700117 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700118 let Slice {
119 bracket: _,
120 inner: inner2,
121 } = other;
122 inner == inner2
123 }
124}
125
126impl Hash for Slice {
127 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnayeb952ba2020-04-14 15:02:24 -0700128 let Slice { bracket: _, inner } = self;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700129 inner.hash(state);
130 }
131}
132
David Tolnay417305a2020-03-18 13:54:00 -0700133impl Eq for Signature {}
134
135impl PartialEq for Signature {
136 fn eq(&self, other: &Signature) -> bool {
137 let Signature {
138 fn_token: _,
139 receiver,
140 args,
141 ret,
142 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700143 paren_token: _,
144 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700145 } = self;
146 let Signature {
147 fn_token: _,
148 receiver: receiver2,
149 args: args2,
150 ret: ret2,
151 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700152 paren_token: _,
153 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700154 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700155 receiver == receiver2
156 && ret == ret2
157 && throws == throws2
158 && args.len() == args2.len()
159 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700160 }
161}
162
163impl Hash for Signature {
164 fn hash<H: Hasher>(&self, state: &mut H) {
165 let Signature {
166 fn_token: _,
167 receiver,
168 args,
169 ret,
170 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700171 paren_token: _,
172 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700173 } = self;
174 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700175 for arg in args {
176 arg.hash(state);
177 }
David Tolnay417305a2020-03-18 13:54:00 -0700178 ret.hash(state);
179 throws.hash(state);
180 }
181}
182
183impl Eq for Receiver {}
184
185impl PartialEq for Receiver {
186 fn eq(&self, other: &Receiver) -> bool {
187 let Receiver { mutability, ident } = self;
188 let Receiver {
189 mutability: mutability2,
190 ident: ident2,
191 } = other;
192 mutability.is_some() == mutability2.is_some() && ident == ident2
193 }
194}
195
196impl Hash for Receiver {
197 fn hash<H: Hasher>(&self, state: &mut H) {
198 let Receiver { mutability, ident } = self;
199 mutability.is_some().hash(state);
200 ident.hash(state);
201 }
202}