Skip to content

Commit fee6f71

Browse files
committed
add trait impls to proc_macro::Ident
1 parent a171994 commit fee6f71

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::cmp;
2+
3+
#[derive(Copy, Clone, Hash)]
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+
}

library/proc_macro/src/bridge/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ mod closure;
151151
mod fxhash;
152152
#[forbid(unsafe_code)]
153153
mod handle;
154+
mod ident;
154155
#[macro_use]
155156
#[forbid(unsafe_code)]
156157
mod rpc;
@@ -162,6 +163,7 @@ pub mod server;
162163
mod symbol;
163164

164165
use buffer::Buffer;
166+
pub use ident::Ident;
165167
pub use rpc::PanicMessage;
166168
use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
167169

@@ -471,13 +473,6 @@ pub struct Punct<Span> {
471473

472474
compound_traits!(struct Punct<Span> { ch, joint, span });
473475

474-
#[derive(Copy, Clone, Eq, PartialEq)]
475-
pub struct Ident<Span, Symbol> {
476-
pub sym: Symbol,
477-
pub is_raw: bool,
478-
pub span: Span,
479-
}
480-
481476
compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span });
482477

483478
#[derive(Clone, Eq, PartialEq)]

library/proc_macro/src/bridge/symbol.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
1212
use std::cell::RefCell;
1313
use std::num::NonZero;
14-
use std::str;
14+
use std::{cmp, str};
1515

1616
use super::*;
1717

1818
/// Handle for a symbol string stored within the Interner.
19-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
19+
#[derive(Copy, Clone, Hash)]
2020
pub struct Symbol(NonZero<u32>);
2121

2222
impl !Send for Symbol {}
@@ -97,6 +97,32 @@ impl fmt::Display for Symbol {
9797
}
9898
}
9999

100+
impl PartialEq<Self> for Symbol {
101+
fn eq(&self, other: &Self) -> bool {
102+
self.0 == other.0
103+
}
104+
}
105+
106+
impl PartialEq<str> for Symbol {
107+
fn eq(&self, other: &str) -> bool {
108+
self.with(|s| s == other)
109+
}
110+
}
111+
112+
impl Eq for Symbol {}
113+
114+
impl PartialOrd for Symbol {
115+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
116+
Some(self.cmp(other))
117+
}
118+
}
119+
120+
impl Ord for Symbol {
121+
fn cmp(&self, other: &Self) -> cmp::Ordering {
122+
self.with(|s| other.with(|o| s.cmp(o)))
123+
}
124+
}
125+
100126
impl<S> Encode<S> for Symbol {
101127
fn encode(self, w: &mut Writer, s: &mut S) {
102128
self.with(|sym| sym.encode(w, s))

library/proc_macro/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ impl PartialEq<Punct> for char {
10181018
}
10191019

10201020
/// An identifier (`ident`).
1021-
#[derive(Clone)]
1021+
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
10221022
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10231023
pub struct Ident(bridge::Ident<bridge::client::Span, bridge::client::Symbol>);
10241024

@@ -1100,6 +1100,13 @@ impl fmt::Debug for Ident {
11001100
}
11011101
}
11021102

1103+
#[stable(feature = "proc_macro_ident_impls", since = "CURRENT_RUSTC_VERSION")]
1104+
impl<T: AsRef<str> + ?Sized> PartialEq<T> for Ident {
1105+
fn eq(&self, other: &T) -> bool {
1106+
self.0 == other
1107+
}
1108+
}
1109+
11031110
/// A literal string (`"hello"`), byte string (`b"hello"`), C string (`c"hello"`),
11041111
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
11051112
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).

0 commit comments

Comments
 (0)