blob: 741431a4c45568d364821fc8b4a9376cd57a6749 [file] [log] [blame]
David Tolnayc21b20a2020-03-15 23:25:32 -07001use crate::syntax::{Ref, Ty1, Type};
David Tolnay7db73692019-10-20 14:51:12 -04002use std::hash::{Hash, Hasher};
David Tolnayc21b20a2020-03-15 23:25:32 -07003use std::mem;
4
5impl Hash for Type {
6 fn hash<H: Hasher>(&self, state: &mut H) {
7 mem::discriminant(self).hash(state);
8 match self {
9 Type::Ident(t) => t.hash(state),
10 Type::RustBox(t) => t.hash(state),
11 Type::UniquePtr(t) => t.hash(state),
12 Type::Ref(t) => t.hash(state),
13 Type::Str(t) => t.hash(state),
14 Type::Void(_) => {}
15 }
16 }
17}
18
19impl Eq for Type {}
20
21impl PartialEq for Type {
22 fn eq(&self, other: &Type) -> bool {
23 match (self, other) {
24 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
25 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
26 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
27 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
28 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
29 (Type::Void(_), Type::Void(_)) => true,
30 (_, _) => false,
31 }
32 }
33}
David Tolnay7db73692019-10-20 14:51:12 -040034
35impl Eq for Ty1 {}
36
37impl PartialEq for Ty1 {
38 fn eq(&self, other: &Ty1) -> bool {
39 self.name == other.name && self.inner == other.inner
40 }
41}
42
43impl Hash for Ty1 {
44 fn hash<H: Hasher>(&self, state: &mut H) {
45 self.name.hash(state);
46 self.inner.hash(state);
47 }
48}
49
50impl Eq for Ref {}
51
52impl PartialEq for Ref {
53 fn eq(&self, other: &Ref) -> bool {
54 self.inner == other.inner
55 }
56}
57
58impl Hash for Ref {
59 fn hash<H: Hasher>(&self, state: &mut H) {
60 self.inner.hash(state);
61 }
62}