1 use crate::syntax::{ 2 Array, ExternFn, Include, Lifetimes, Ptr, Receiver, Ref, Signature, SliceRef, Ty1, Type, Var, 3 }; 4 use std::hash::{Hash, Hasher}; 5 use std::mem; 6 use std::ops::{Deref, DerefMut}; 7 8 impl PartialEq for Include { eq(&self, other: &Self) -> bool9 fn eq(&self, other: &Self) -> bool { 10 let Include { 11 cfg: _, 12 path, 13 kind, 14 begin_span: _, 15 end_span: _, 16 } = self; 17 let Include { 18 cfg: _, 19 path: path2, 20 kind: kind2, 21 begin_span: _, 22 end_span: _, 23 } = other; 24 path == path2 && kind == kind2 25 } 26 } 27 28 impl Deref for ExternFn { 29 type Target = Signature; 30 deref(&self) -> &Self::Target31 fn deref(&self) -> &Self::Target { 32 &self.sig 33 } 34 } 35 36 impl DerefMut for ExternFn { deref_mut(&mut self) -> &mut Self::Target37 fn deref_mut(&mut self) -> &mut Self::Target { 38 &mut self.sig 39 } 40 } 41 42 impl Hash for Type { hash<H: Hasher>(&self, state: &mut H)43 fn hash<H: Hasher>(&self, state: &mut H) { 44 mem::discriminant(self).hash(state); 45 match self { 46 Type::Ident(t) => t.hash(state), 47 Type::RustBox(t) => t.hash(state), 48 Type::UniquePtr(t) => t.hash(state), 49 Type::SharedPtr(t) => t.hash(state), 50 Type::WeakPtr(t) => t.hash(state), 51 Type::Ref(t) => t.hash(state), 52 Type::Ptr(t) => t.hash(state), 53 Type::Str(t) => t.hash(state), 54 Type::RustVec(t) => t.hash(state), 55 Type::CxxVector(t) => t.hash(state), 56 Type::Fn(t) => t.hash(state), 57 Type::SliceRef(t) => t.hash(state), 58 Type::Array(t) => t.hash(state), 59 Type::Void(_) => {} 60 } 61 } 62 } 63 64 impl Eq for Type {} 65 66 impl PartialEq for Type { eq(&self, other: &Self) -> bool67 fn eq(&self, other: &Self) -> bool { 68 match (self, other) { 69 (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs, 70 (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs, 71 (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs, 72 (Type::SharedPtr(lhs), Type::SharedPtr(rhs)) => lhs == rhs, 73 (Type::WeakPtr(lhs), Type::WeakPtr(rhs)) => lhs == rhs, 74 (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs, 75 (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs, 76 (Type::RustVec(lhs), Type::RustVec(rhs)) => lhs == rhs, 77 (Type::CxxVector(lhs), Type::CxxVector(rhs)) => lhs == rhs, 78 (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs, 79 (Type::SliceRef(lhs), Type::SliceRef(rhs)) => lhs == rhs, 80 (Type::Void(_), Type::Void(_)) => true, 81 (_, _) => false, 82 } 83 } 84 } 85 86 impl Eq for Lifetimes {} 87 88 impl PartialEq for Lifetimes { eq(&self, other: &Self) -> bool89 fn eq(&self, other: &Self) -> bool { 90 let Lifetimes { 91 lt_token: _, 92 lifetimes, 93 gt_token: _, 94 } = self; 95 let Lifetimes { 96 lt_token: _, 97 lifetimes: lifetimes2, 98 gt_token: _, 99 } = other; 100 lifetimes.iter().eq(lifetimes2) 101 } 102 } 103 104 impl Hash for Lifetimes { hash<H: Hasher>(&self, state: &mut H)105 fn hash<H: Hasher>(&self, state: &mut H) { 106 let Lifetimes { 107 lt_token: _, 108 lifetimes, 109 gt_token: _, 110 } = self; 111 lifetimes.len().hash(state); 112 for lifetime in lifetimes { 113 lifetime.hash(state); 114 } 115 } 116 } 117 118 impl Eq for Ty1 {} 119 120 impl PartialEq for Ty1 { eq(&self, other: &Self) -> bool121 fn eq(&self, other: &Self) -> bool { 122 let Ty1 { 123 name, 124 langle: _, 125 inner, 126 rangle: _, 127 } = self; 128 let Ty1 { 129 name: name2, 130 langle: _, 131 inner: inner2, 132 rangle: _, 133 } = other; 134 name == name2 && inner == inner2 135 } 136 } 137 138 impl Hash for Ty1 { hash<H: Hasher>(&self, state: &mut H)139 fn hash<H: Hasher>(&self, state: &mut H) { 140 let Ty1 { 141 name, 142 langle: _, 143 inner, 144 rangle: _, 145 } = self; 146 name.hash(state); 147 inner.hash(state); 148 } 149 } 150 151 impl Eq for Ref {} 152 153 impl PartialEq for Ref { eq(&self, other: &Self) -> bool154 fn eq(&self, other: &Self) -> bool { 155 let Ref { 156 pinned, 157 ampersand: _, 158 lifetime, 159 mutable, 160 inner, 161 pin_tokens: _, 162 mutability: _, 163 } = self; 164 let Ref { 165 pinned: pinned2, 166 ampersand: _, 167 lifetime: lifetime2, 168 mutable: mutable2, 169 inner: inner2, 170 pin_tokens: _, 171 mutability: _, 172 } = other; 173 pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && inner == inner2 174 } 175 } 176 177 impl Hash for Ref { hash<H: Hasher>(&self, state: &mut H)178 fn hash<H: Hasher>(&self, state: &mut H) { 179 let Ref { 180 pinned, 181 ampersand: _, 182 lifetime, 183 mutable, 184 inner, 185 pin_tokens: _, 186 mutability: _, 187 } = self; 188 pinned.hash(state); 189 lifetime.hash(state); 190 mutable.hash(state); 191 inner.hash(state); 192 } 193 } 194 195 impl Eq for Ptr {} 196 197 impl PartialEq for Ptr { eq(&self, other: &Ptr) -> bool198 fn eq(&self, other: &Ptr) -> bool { 199 let Ptr { 200 star: _, 201 mutable, 202 inner, 203 mutability: _, 204 constness: _, 205 } = self; 206 let Ptr { 207 star: _, 208 mutable: mutable2, 209 inner: inner2, 210 mutability: _, 211 constness: _, 212 } = other; 213 mutable == mutable2 && inner == inner2 214 } 215 } 216 217 impl Hash for Ptr { hash<H: Hasher>(&self, state: &mut H)218 fn hash<H: Hasher>(&self, state: &mut H) { 219 let Ptr { 220 star: _, 221 mutable, 222 inner, 223 mutability: _, 224 constness: _, 225 } = self; 226 mutable.hash(state); 227 inner.hash(state); 228 } 229 } 230 231 impl Eq for SliceRef {} 232 233 impl PartialEq for SliceRef { eq(&self, other: &Self) -> bool234 fn eq(&self, other: &Self) -> bool { 235 let SliceRef { 236 ampersand: _, 237 lifetime, 238 mutable, 239 bracket: _, 240 inner, 241 mutability: _, 242 } = self; 243 let SliceRef { 244 ampersand: _, 245 lifetime: lifetime2, 246 mutable: mutable2, 247 bracket: _, 248 inner: inner2, 249 mutability: _, 250 } = other; 251 lifetime == lifetime2 && mutable == mutable2 && inner == inner2 252 } 253 } 254 255 impl Hash for SliceRef { hash<H: Hasher>(&self, state: &mut H)256 fn hash<H: Hasher>(&self, state: &mut H) { 257 let SliceRef { 258 ampersand: _, 259 lifetime, 260 mutable, 261 bracket: _, 262 inner, 263 mutability: _, 264 } = self; 265 lifetime.hash(state); 266 mutable.hash(state); 267 inner.hash(state); 268 } 269 } 270 271 impl Eq for Array {} 272 273 impl PartialEq for Array { eq(&self, other: &Self) -> bool274 fn eq(&self, other: &Self) -> bool { 275 let Array { 276 bracket: _, 277 inner, 278 semi_token: _, 279 len, 280 len_token: _, 281 } = self; 282 let Array { 283 bracket: _, 284 inner: inner2, 285 semi_token: _, 286 len: len2, 287 len_token: _, 288 } = other; 289 inner == inner2 && len == len2 290 } 291 } 292 293 impl Hash for Array { hash<H: Hasher>(&self, state: &mut H)294 fn hash<H: Hasher>(&self, state: &mut H) { 295 let Array { 296 bracket: _, 297 inner, 298 semi_token: _, 299 len, 300 len_token: _, 301 } = self; 302 inner.hash(state); 303 len.hash(state); 304 } 305 } 306 307 impl Eq for Signature {} 308 309 impl PartialEq for Signature { eq(&self, other: &Self) -> bool310 fn eq(&self, other: &Self) -> bool { 311 let Signature { 312 asyncness, 313 unsafety, 314 fn_token: _, 315 generics: _, 316 receiver, 317 args, 318 ret, 319 throws, 320 paren_token: _, 321 throws_tokens: _, 322 } = self; 323 let Signature { 324 asyncness: asyncness2, 325 unsafety: unsafety2, 326 fn_token: _, 327 generics: _, 328 receiver: receiver2, 329 args: args2, 330 ret: ret2, 331 throws: throws2, 332 paren_token: _, 333 throws_tokens: _, 334 } = other; 335 asyncness.is_some() == asyncness2.is_some() 336 && unsafety.is_some() == unsafety2.is_some() 337 && receiver == receiver2 338 && ret == ret2 339 && throws == throws2 340 && args.len() == args2.len() 341 && args.iter().zip(args2).all(|(arg, arg2)| { 342 let Var { 343 cfg: _, 344 doc: _, 345 attrs: _, 346 visibility: _, 347 name: _, 348 colon_token: _, 349 ty, 350 } = arg; 351 let Var { 352 cfg: _, 353 doc: _, 354 attrs: _, 355 visibility: _, 356 name: _, 357 colon_token: _, 358 ty: ty2, 359 } = arg2; 360 ty == ty2 361 }) 362 } 363 } 364 365 impl Hash for Signature { hash<H: Hasher>(&self, state: &mut H)366 fn hash<H: Hasher>(&self, state: &mut H) { 367 let Signature { 368 asyncness, 369 unsafety, 370 fn_token: _, 371 generics: _, 372 receiver, 373 args, 374 ret, 375 throws, 376 paren_token: _, 377 throws_tokens: _, 378 } = self; 379 asyncness.is_some().hash(state); 380 unsafety.is_some().hash(state); 381 receiver.hash(state); 382 for arg in args { 383 let Var { 384 cfg: _, 385 doc: _, 386 attrs: _, 387 visibility: _, 388 name: _, 389 colon_token: _, 390 ty, 391 } = arg; 392 ty.hash(state); 393 } 394 ret.hash(state); 395 throws.hash(state); 396 } 397 } 398 399 impl Eq for Receiver {} 400 401 impl PartialEq for Receiver { eq(&self, other: &Self) -> bool402 fn eq(&self, other: &Self) -> bool { 403 let Receiver { 404 pinned, 405 ampersand: _, 406 lifetime, 407 mutable, 408 var: _, 409 colon_token: _, 410 ty, 411 shorthand: _, 412 pin_tokens: _, 413 mutability: _, 414 } = self; 415 let Receiver { 416 pinned: pinned2, 417 ampersand: _, 418 lifetime: lifetime2, 419 mutable: mutable2, 420 var: _, 421 colon_token: _, 422 ty: ty2, 423 shorthand: _, 424 pin_tokens: _, 425 mutability: _, 426 } = other; 427 pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && ty == ty2 428 } 429 } 430 431 impl Hash for Receiver { hash<H: Hasher>(&self, state: &mut H)432 fn hash<H: Hasher>(&self, state: &mut H) { 433 let Receiver { 434 pinned, 435 ampersand: _, 436 lifetime, 437 mutable, 438 var: _, 439 colon_token: _, 440 ty, 441 shorthand: _, 442 pin_tokens: _, 443 mutability: _, 444 } = self; 445 pinned.hash(state); 446 lifetime.hash(state); 447 mutable.hash(state); 448 ty.hash(state); 449 } 450 } 451