blob: 27aa43ac5ddc686c232d0b8b797348c2d84aceeb [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 Tolnayd95b1192020-03-18 20:07:46 -0700119 tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700120 } = self;
121 let Signature {
122 fn_token: _,
123 receiver: receiver2,
124 args: args2,
125 ret: ret2,
126 throws: throws2,
David Tolnayd95b1192020-03-18 20:07:46 -0700127 tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700128 } = other;
129 receiver == receiver2 && args == args2 && ret == ret2 && throws == throws2
130 }
131}
132
133impl Hash for Signature {
134 fn hash<H: Hasher>(&self, state: &mut H) {
135 let Signature {
136 fn_token: _,
137 receiver,
138 args,
139 ret,
140 throws,
David Tolnayd95b1192020-03-18 20:07:46 -0700141 tokens: _,
David Tolnay417305a2020-03-18 13:54:00 -0700142 } = self;
143 receiver.hash(state);
144 args.hash(state);
145 ret.hash(state);
146 throws.hash(state);
147 }
148}
149
150impl Eq for Receiver {}
151
152impl PartialEq for Receiver {
153 fn eq(&self, other: &Receiver) -> bool {
154 let Receiver { mutability, ident } = self;
155 let Receiver {
156 mutability: mutability2,
157 ident: ident2,
158 } = other;
159 mutability.is_some() == mutability2.is_some() && ident == ident2
160 }
161}
162
163impl Hash for Receiver {
164 fn hash<H: Hasher>(&self, state: &mut H) {
165 let Receiver { mutability, ident } = self;
166 mutability.is_some().hash(state);
167 ident.hash(state);
168 }
169}