blob: dfaab550e980dda52756ef6fba17ae5d750ae54c [file] [log] [blame]
David Tolnay16448732020-03-18 12:39:36 -07001use crate::syntax::{ExternFn, 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),
23 Type::Void(_) => {}
24 }
25 }
26}
27
28impl Eq for Type {}
29
30impl PartialEq for Type {
31 fn eq(&self, other: &Type) -> bool {
32 match (self, other) {
33 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
34 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
35 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
36 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
37 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
38 (Type::Void(_), Type::Void(_)) => true,
39 (_, _) => false,
40 }
41 }
42}
David Tolnay7db73692019-10-20 14:51:12 -040043
44impl Eq for Ty1 {}
45
46impl PartialEq for Ty1 {
47 fn eq(&self, other: &Ty1) -> bool {
48 self.name == other.name && self.inner == other.inner
49 }
50}
51
52impl Hash for Ty1 {
53 fn hash<H: Hasher>(&self, state: &mut H) {
54 self.name.hash(state);
55 self.inner.hash(state);
56 }
57}
58
59impl Eq for Ref {}
60
61impl PartialEq for Ref {
62 fn eq(&self, other: &Ref) -> bool {
63 self.inner == other.inner
64 }
65}
66
67impl Hash for Ref {
68 fn hash<H: Hasher>(&self, state: &mut H) {
69 self.inner.hash(state);
70 }
71}