| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame^] | 1 | use crate::syntax::{ExternFn, Receiver, Ref, Signature, Slice, 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), |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 23 | Type::Fn(t) => t.hash(state), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame^] | 24 | Type::Slice(t) => t.hash(state), |
| 25 | Type::SliceRefU8(t) => t.hash(state), |
| David Tolnay | c21b20a | 2020-03-15 23:25:32 -0700 | [diff] [blame] | 26 | Type::Void(_) => {} |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Eq for Type {} |
| 32 | |
| 33 | impl PartialEq for Type { |
| 34 | fn eq(&self, other: &Type) -> bool { |
| 35 | match (self, other) { |
| 36 | (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs, |
| 37 | (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs, |
| 38 | (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs, |
| 39 | (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs, |
| 40 | (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs, |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 41 | (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs, |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame^] | 42 | (Type::Slice(lhs), Type::Slice(rhs)) => lhs == rhs, |
| 43 | (Type::SliceRefU8(lhs), Type::SliceRefU8(rhs)) => lhs == rhs, |
| David Tolnay | c21b20a | 2020-03-15 23:25:32 -0700 | [diff] [blame] | 44 | (Type::Void(_), Type::Void(_)) => true, |
| 45 | (_, _) => false, |
| 46 | } |
| 47 | } |
| 48 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 49 | |
| 50 | impl Eq for Ty1 {} |
| 51 | |
| 52 | impl PartialEq for Ty1 { |
| 53 | fn eq(&self, other: &Ty1) -> bool { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame] | 54 | let Ty1 { |
| 55 | name, |
| 56 | langle: _, |
| 57 | inner, |
| 58 | rangle: _, |
| 59 | } = self; |
| 60 | let Ty1 { |
| 61 | name: name2, |
| 62 | langle: _, |
| 63 | inner: inner2, |
| 64 | rangle: _, |
| 65 | } = other; |
| 66 | name == name2 && inner == inner2 |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | impl Hash for Ty1 { |
| 71 | fn hash<H: Hasher>(&self, state: &mut H) { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame] | 72 | let Ty1 { |
| 73 | name, |
| 74 | langle: _, |
| 75 | inner, |
| 76 | rangle: _, |
| 77 | } = self; |
| 78 | name.hash(state); |
| 79 | inner.hash(state); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Eq for Ref {} |
| 84 | |
| 85 | impl PartialEq for Ref { |
| 86 | fn eq(&self, other: &Ref) -> bool { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame] | 87 | let Ref { |
| 88 | ampersand: _, |
| 89 | mutability, |
| 90 | inner, |
| 91 | } = self; |
| 92 | let Ref { |
| 93 | ampersand: _, |
| 94 | mutability: mutability2, |
| 95 | inner: inner2, |
| 96 | } = other; |
| 97 | mutability.is_some() == mutability2.is_some() && inner == inner2 |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | impl Hash for Ref { |
| 102 | fn hash<H: Hasher>(&self, state: &mut H) { |
| David Tolnay | 35c82eb | 2020-03-18 16:48:28 -0700 | [diff] [blame] | 103 | let Ref { |
| 104 | ampersand: _, |
| 105 | mutability, |
| 106 | inner, |
| 107 | } = self; |
| 108 | mutability.is_some().hash(state); |
| 109 | inner.hash(state); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 110 | } |
| 111 | } |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 112 | |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame^] | 113 | impl Eq for Slice {} |
| 114 | |
| 115 | impl PartialEq for Slice { |
| 116 | fn eq(&self, other: &Slice) -> bool { |
| 117 | let Slice { |
| 118 | bracket: _, |
| 119 | inner, |
| 120 | } = self; |
| 121 | let Slice { |
| 122 | bracket: _, |
| 123 | inner: inner2, |
| 124 | } = other; |
| 125 | inner == inner2 |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl Hash for Slice { |
| 130 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 131 | let Slice { |
| 132 | bracket: _, |
| 133 | inner, |
| 134 | } = self; |
| 135 | inner.hash(state); |
| 136 | } |
| 137 | } |
| 138 | |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 139 | impl Eq for Signature {} |
| 140 | |
| 141 | impl PartialEq for Signature { |
| 142 | fn eq(&self, other: &Signature) -> bool { |
| 143 | let Signature { |
| 144 | fn_token: _, |
| 145 | receiver, |
| 146 | args, |
| 147 | ret, |
| 148 | throws, |
| David Tolnay | e3a4815 | 2020-04-08 19:38:05 -0700 | [diff] [blame] | 149 | paren_token: _, |
| 150 | throws_tokens: _, |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 151 | } = self; |
| 152 | let Signature { |
| 153 | fn_token: _, |
| 154 | receiver: receiver2, |
| 155 | args: args2, |
| 156 | ret: ret2, |
| 157 | throws: throws2, |
| David Tolnay | e3a4815 | 2020-04-08 19:38:05 -0700 | [diff] [blame] | 158 | paren_token: _, |
| 159 | throws_tokens: _, |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 160 | } = other; |
| David Tolnay | e3a4815 | 2020-04-08 19:38:05 -0700 | [diff] [blame] | 161 | receiver == receiver2 |
| 162 | && ret == ret2 |
| 163 | && throws == throws2 |
| 164 | && args.len() == args2.len() |
| 165 | && args.iter().zip(args2).all(|(arg, arg2)| arg == arg2) |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | impl Hash for Signature { |
| 170 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 171 | let Signature { |
| 172 | fn_token: _, |
| 173 | receiver, |
| 174 | args, |
| 175 | ret, |
| 176 | throws, |
| David Tolnay | e3a4815 | 2020-04-08 19:38:05 -0700 | [diff] [blame] | 177 | paren_token: _, |
| 178 | throws_tokens: _, |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 179 | } = self; |
| 180 | receiver.hash(state); |
| David Tolnay | e3a4815 | 2020-04-08 19:38:05 -0700 | [diff] [blame] | 181 | for arg in args { |
| 182 | arg.hash(state); |
| 183 | } |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 184 | ret.hash(state); |
| 185 | throws.hash(state); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Eq for Receiver {} |
| 190 | |
| 191 | impl PartialEq for Receiver { |
| 192 | fn eq(&self, other: &Receiver) -> bool { |
| 193 | let Receiver { mutability, ident } = self; |
| 194 | let Receiver { |
| 195 | mutability: mutability2, |
| 196 | ident: ident2, |
| 197 | } = other; |
| 198 | mutability.is_some() == mutability2.is_some() && ident == ident2 |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl Hash for Receiver { |
| 203 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 204 | let Receiver { mutability, ident } = self; |
| 205 | mutability.is_some().hash(state); |
| 206 | ident.hash(state); |
| 207 | } |
| 208 | } |