blob: 34fb851a476f3900beeee8ff96c4f788b1028479 [file] [log] [blame]
David Tolnay417305a2020-03-18 13:54:00 -07001use crate::syntax::{ExternFn, Receiver, Ref, Signature, 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),
David Tolnayc21b20a2020-03-15 23:25:32 -070024 Type::Void(_) => {}
25 }
26 }
27}
28
29impl Eq for Type {}
30
31impl PartialEq for Type {
32 fn eq(&self, other: &Type) -> bool {
33 match (self, other) {
34 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
35 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
36 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
37 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
38 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
David Tolnay417305a2020-03-18 13:54:00 -070039 (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs,
David Tolnayc21b20a2020-03-15 23:25:32 -070040 (Type::Void(_), Type::Void(_)) => true,
41 (_, _) => false,
42 }
43 }
44}
David Tolnay7db73692019-10-20 14:51:12 -040045
46impl Eq for Ty1 {}
47
48impl PartialEq for Ty1 {
49 fn eq(&self, other: &Ty1) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070050 let Ty1 {
51 name,
52 langle: _,
53 inner,
54 rangle: _,
55 } = self;
56 let Ty1 {
57 name: name2,
58 langle: _,
59 inner: inner2,
60 rangle: _,
61 } = other;
62 name == name2 && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040063 }
64}
65
66impl Hash for Ty1 {
67 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070068 let Ty1 {
69 name,
70 langle: _,
71 inner,
72 rangle: _,
73 } = self;
74 name.hash(state);
75 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77}
78
79impl Eq for Ref {}
80
81impl PartialEq for Ref {
82 fn eq(&self, other: &Ref) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070083 let Ref {
84 ampersand: _,
85 mutability,
86 inner,
87 } = self;
88 let Ref {
89 ampersand: _,
90 mutability: mutability2,
91 inner: inner2,
92 } = other;
93 mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95}
96
97impl Hash for Ref {
98 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070099 let Ref {
100 ampersand: _,
101 mutability,
102 inner,
103 } = self;
104 mutability.is_some().hash(state);
105 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400106 }
107}
David Tolnay417305a2020-03-18 13:54:00 -0700108
109impl Eq for Signature {}
110
111impl PartialEq for Signature {
112 fn eq(&self, other: &Signature) -> bool {
113 let Signature {
114 fn_token: _,
115 receiver,
116 args,
117 ret,
118 throws,
David Tolnaye3a48152020-04-08 19:38:05 -0700119 paren_token: _,
120 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700121 } = self;
122 let Signature {
123 fn_token: _,
124 receiver: receiver2,
125 args: args2,
126 ret: ret2,
127 throws: throws2,
David Tolnaye3a48152020-04-08 19:38:05 -0700128 paren_token: _,
129 throws_tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700130 } = other;
David Tolnaye3a48152020-04-08 19:38:05 -0700131 receiver == receiver2
132 && ret == ret2
133 && throws == throws2
134 && args.len() == args2.len()
135 && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2)
David Tolnay417305a2020-03-18 13:54:00 -0700136 }
137}
138
139impl Hash for Signature {
140 fn hash<H: Hasher>(&self, state: &mut H) {
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 receiver.hash(state);
David Tolnaye3a48152020-04-08 19:38:05 -0700151 for arg in args {
152 arg.hash(state);
153 }
David Tolnay417305a2020-03-18 13:54:00 -0700154 ret.hash(state);
155 throws.hash(state);
156 }
157}
158
159impl Eq for Receiver {}
160
161impl PartialEq for Receiver {
162 fn eq(&self, other: &Receiver) -> bool {
163 let Receiver { mutability, ident } = self;
164 let Receiver {
165 mutability: mutability2,
166 ident: ident2,
167 } = other;
168 mutability.is_some() == mutability2.is_some() && ident == ident2
169 }
170}
171
172impl Hash for Receiver {
173 fn hash<H: Hasher>(&self, state: &mut H) {
174 let Receiver { mutability, ident } = self;
175 mutability.is_some().hash(state);
176 ident.hash(state);
177 }
178}