|
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,42 @@ 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: fmt::Display, T> PartialEq<T> for Ident<Span, Symbol> |
| 460 | +where |
| 461 | + Symbol: PartialEq<str>, |
| 462 | + T: AsRef<str> + ?Sized, |
| 463 | +{ |
| 464 | + fn eq(&self, other: &T) -> bool { |
| 465 | + self.to_string() == other.as_ref() |
| 466 | + } |
| 467 | +} |
| 468 | + |
| 469 | +impl<Span, Symbol: Hash> Hash for Ident<Span, Symbol> { |
| 470 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 471 | + self.sym.hash(state); |
| 472 | + self.is_raw.hash(state); |
| 473 | + } |
| 474 | +} |
| 475 | + |
| 476 | +/// Prints the identifier as a string that should be losslessly convertible back |
| 477 | +/// into the same identifier. |
| 478 | +#[stable(feature = "proc_macro_lib2", since = "1.29.0")] |
| 479 | +impl<Span, Symbol: fmt::Display> fmt::Display for Ident<Span, Symbol> { |
| 480 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 481 | + if self.is_raw { |
| 482 | + f.write_str("r#")?; |
| 483 | + } |
| 484 | + fmt::Display::fmt(&self.sym, f) |
| 485 | + } |
| 486 | +} |
| 487 | + |
459 | 488 | compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span }); |
460 | 489 |
|
461 | 490 | #[derive(Clone, Eq, PartialEq)] |
|
0 commit comments