| David Tolnay | c21b20a | 2020-03-15 23:25:32 -0700 | [diff] [blame^] | 1 | use crate::syntax::{Ref, 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; |
| 4 | |
| 5 | impl 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 | |
| 19 | impl Eq for Type {} |
| 20 | |
| 21 | impl 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 34 | |
| 35 | impl Eq for Ty1 {} |
| 36 | |
| 37 | impl PartialEq for Ty1 { |
| 38 | fn eq(&self, other: &Ty1) -> bool { |
| 39 | self.name == other.name && self.inner == other.inner |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl 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 | |
| 50 | impl Eq for Ref {} |
| 51 | |
| 52 | impl PartialEq for Ref { |
| 53 | fn eq(&self, other: &Ref) -> bool { |
| 54 | self.inner == other.inner |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl Hash for Ref { |
| 59 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 60 | self.inner.hash(state); |
| 61 | } |
| 62 | } |