blob: 6baa122a84101a6fe3d4482bd8d9aa41fc33c910 [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 {
David Tolnay35c82eb2020-03-18 16:48:28 -070048 let Ty1 {
49 name,
50 langle: _,
51 inner,
52 rangle: _,
53 } = self;
54 let Ty1 {
55 name: name2,
56 langle: _,
57 inner: inner2,
58 rangle: _,
59 } = other;
60 name == name2 && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040061 }
62}
63
64impl Hash for Ty1 {
65 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070066 let Ty1 {
67 name,
68 langle: _,
69 inner,
70 rangle: _,
71 } = self;
72 name.hash(state);
73 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -040074 }
75}
76
77impl Eq for Ref {}
78
79impl PartialEq for Ref {
80 fn eq(&self, other: &Ref) -> bool {
David Tolnay35c82eb2020-03-18 16:48:28 -070081 let Ref {
82 ampersand: _,
83 mutability,
84 inner,
85 } = self;
86 let Ref {
87 ampersand: _,
88 mutability: mutability2,
89 inner: inner2,
90 } = other;
91 mutability.is_some() == mutability2.is_some() && inner == inner2
David Tolnay7db73692019-10-20 14:51:12 -040092 }
93}
94
95impl Hash for Ref {
96 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay35c82eb2020-03-18 16:48:28 -070097 let Ref {
98 ampersand: _,
99 mutability,
100 inner,
101 } = self;
102 mutability.is_some().hash(state);
103 inner.hash(state);
David Tolnay7db73692019-10-20 14:51:12 -0400104 }
105}