|
8 | 8 |
|
9 | 9 | #![deny(unsafe_code)] |
10 | 10 |
|
11 | | -use std::hash::Hash; |
| 11 | +use std::hash::{Hash, Hasher}; |
12 | 12 | use std::ops::{Bound, Range}; |
13 | 13 | use std::sync::Once; |
14 | 14 | use std::{fmt, marker, mem, panic, thread}; |
@@ -449,13 +449,69 @@ pub struct Punct<Span> { |
449 | 449 |
|
450 | 450 | compound_traits!(struct Punct<Span> { ch, joint, span }); |
451 | 451 |
|
452 | | -#[derive(Copy, Clone, Eq, PartialEq)] |
| 452 | +#[derive(Copy, Clone)] |
453 | 453 | pub struct Ident<Span, Symbol> { |
454 | 454 | pub sym: Symbol, |
455 | 455 | pub is_raw: bool, |
456 | 456 | pub span: Span, |
457 | 457 | } |
458 | 458 |
|
| 459 | +impl<Span, Symbol> PartialEq for Ident<Span, Symbol> |
| 460 | +where |
| 461 | + Symbol: PartialEq, |
| 462 | +{ |
| 463 | + fn eq(&self, other: &Self) -> bool { |
| 464 | + self.sym == other.sym && self.is_raw == other.is_raw |
| 465 | + } |
| 466 | +} |
| 467 | + |
| 468 | +impl<Span, Symbol> PartialEq<String> for Ident<Span, Symbol> |
| 469 | +where |
| 470 | + Symbol: PartialEq<str>, |
| 471 | +{ |
| 472 | + fn eq(&self, other: &String) -> bool { |
| 473 | + if self.is_raw { |
| 474 | + other.strip_prefix("r#").is_some_and(|s| self.sym == *s) |
| 475 | + } else { |
| 476 | + self.sym == **other |
| 477 | + } |
| 478 | + } |
| 479 | +} |
| 480 | + |
| 481 | +impl<Span, Symbol> PartialEq<str> for Ident<Span, Symbol> |
| 482 | +where |
| 483 | + Symbol: PartialEq<str>, |
| 484 | +{ |
| 485 | + fn eq(&self, other: &str) -> bool { |
| 486 | + if self.is_raw { |
| 487 | + other.strip_prefix("r#").is_some_and(|s| self.sym == *s) |
| 488 | + } else { |
| 489 | + self.sym == *other |
| 490 | + } |
| 491 | + } |
| 492 | +} |
| 493 | + |
| 494 | +impl<Span, Symbol> Eq for Ident<Span, Symbol> where Symbol: PartialEq {} |
| 495 | + |
| 496 | +impl<Span, Symbol: Hash> Hash for Ident<Span, Symbol> { |
| 497 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 498 | + self.sym.hash(state); |
| 499 | + self.is_raw.hash(state); |
| 500 | + } |
| 501 | +} |
| 502 | + |
| 503 | +/// Prints the identifier as a string that should be losslessly convertible back |
| 504 | +/// into the same identifier. |
| 505 | +#[stable(feature = "proc_macro_lib2", since = "1.29.0")] |
| 506 | +impl<Span, Symbol: fmt::Display> fmt::Display for Ident<Span, Symbol> { |
| 507 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 508 | + if self.is_raw { |
| 509 | + f.write_str("r#")?; |
| 510 | + } |
| 511 | + fmt::Display::fmt(&self.sym, f) |
| 512 | + } |
| 513 | +} |
| 514 | + |
459 | 515 | compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span }); |
460 | 516 |
|
461 | 517 | #[derive(Clone, Eq, PartialEq)] |
|
0 commit comments