| David Tolnay | 1644873 | 2020-03-18 12:39:36 -0700 | [diff] [blame] | 1 | use crate::syntax::{ExternFn, Ref, Signature, Ty1, Type}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | use std::hash::{Hash, Hasher}; |
| David Tolnay | c21b20a | 2020-03-15 23:25:32 -0700 | [diff] [blame] | 3 | use std::mem; |
| David Tolnay | 1644873 | 2020-03-18 12:39:36 -0700 | [diff] [blame] | 4 | use std::ops::Deref; |
| 5 | |
| 6 | impl Deref for ExternFn { |
| 7 | type Target = Signature; |
| 8 | |
| 9 | fn deref(&self) -> &Self::Target { |
| 10 | &self.sig |
| 11 | } |
| 12 | } |
| David Tolnay | c21b20a | 2020-03-15 23:25:32 -0700 | [diff] [blame] | 13 | |
| 14 | impl 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 | |
| 28 | impl Eq for Type {} |
| 29 | |
| 30 | impl 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 43 | |
| 44 | impl Eq for Ty1 {} |
| 45 | |
| 46 | impl PartialEq for Ty1 { |
| 47 | fn eq(&self, other: &Ty1) -> bool { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame^] | 48 | 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Hash for Ty1 { |
| 65 | fn hash<H: Hasher>(&self, state: &mut H) { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame^] | 66 | let Ty1 { |
| 67 | name, |
| 68 | langle: _, |
| 69 | inner, |
| 70 | rangle: _, |
| 71 | } = self; |
| 72 | name.hash(state); |
| 73 | inner.hash(state); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Eq for Ref {} |
| 78 | |
| 79 | impl PartialEq for Ref { |
| 80 | fn eq(&self, other: &Ref) -> bool { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame^] | 81 | 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Hash for Ref { |
| 96 | fn hash<H: Hasher>(&self, state: &mut H) { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame^] | 97 | let Ref { |
| 98 | ampersand: _, |
| 99 | mutability, |
| 100 | inner, |
| 101 | } = self; |
| 102 | mutability.is_some().hash(state); |
| 103 | inner.hash(state); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 104 | } |
| 105 | } |