blob: 57b42d11607bda81f6c0b41b5132a606986a261a [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,
119 } = self;
120 let Signature {
121 fn_token: _,
122 receiver: receiver2,
123 args: args2,
124 ret: ret2,
125 throws: throws2,
126 } = other;
127 receiver == receiver2 && args == args2 && ret == ret2 && throws == throws2
128 }
129}
130
131impl Hash for Signature {
132 fn hash<H: Hasher>(&self, state: &mut H) {
133 let Signature {
134 fn_token: _,
135 receiver,
136 args,
137 ret,
138 throws,
139 } = self;
140 receiver.hash(state);
141 args.hash(state);
142 ret.hash(state);
143 throws.hash(state);
144 }
145}
146
147impl Eq for Receiver {}
148
149impl PartialEq for Receiver {
150 fn eq(&self, other: &Receiver) -> bool {
151 let Receiver { mutability, ident } = self;
152 let Receiver {
153 mutability: mutability2,
154 ident: ident2,
155 } = other;
156 mutability.is_some() == mutability2.is_some() && ident == ident2
157 }
158}
159
160impl Hash for Receiver {
161 fn hash<H: Hasher>(&self, state: &mut H) {
162 let Receiver { mutability, ident } = self;
163 mutability.is_some().hash(state);
164 ident.hash(state);
165 }
166}