|
| 1 | +use std::{cmp, hash}; |
| 2 | + |
| 3 | +#[derive(Copy, Clone)] |
| 4 | +pub struct Ident<Span, Symbol> { |
| 5 | + pub sym: Symbol, |
| 6 | + pub is_raw: bool, |
| 7 | + pub span: Span, |
| 8 | +} |
| 9 | + |
| 10 | +impl<Span, Symbol: PartialEq> PartialEq<Self> for Ident<Span, Symbol> { |
| 11 | + fn eq(&self, other: &Self) -> bool { |
| 12 | + self.sym == other.sym && self.is_raw == other.is_raw |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl<Span, Symbol, T> PartialEq<T> for Ident<Span, Symbol> |
| 17 | +where |
| 18 | + Symbol: PartialEq<str>, |
| 19 | + T: AsRef<str> + ?Sized, |
| 20 | +{ |
| 21 | + fn eq(&self, other: &T) -> bool { |
| 22 | + if self.is_raw { |
| 23 | + if let Some(inner) = other.as_ref().strip_prefix("r#") { |
| 24 | + self.sym == *inner |
| 25 | + } else { |
| 26 | + false |
| 27 | + } |
| 28 | + } else { |
| 29 | + self.sym == *other.as_ref() |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<Span, Symbol: Eq> Eq for Ident<Span, Symbol> {} |
| 35 | + |
| 36 | +impl<Span, Symbol: Ord> PartialOrd for Ident<Span, Symbol> { |
| 37 | + fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
| 38 | + Some(self.cmp(other)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<Span, Symbol: Ord> Ord for Ident<Span, Symbol> { |
| 43 | + fn cmp(&self, other: &Self) -> cmp::Ordering { |
| 44 | + self.sym.cmp(&other.sym).then_with(|| self.is_raw.cmp(&other.is_raw)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<Span, Symbol: hash::Hash> hash::Hash for Ident<Span, Symbol> { |
| 49 | + fn hash<H: hash::Hasher>(&self, state: &mut H) { |
| 50 | + self.sym.hash(state); |
| 51 | + self.is_raw.hash(state); |
| 52 | + } |
| 53 | +} |
0 commit comments