Skip to content

Commit 8658174

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

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::{fmt, 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: fmt::Display, T> PartialEq<T> for Ident<Span, Symbol>
11+
where
12+
Symbol: PartialEq<str>,
13+
T: AsRef<str> + ?Sized,
14+
{
15+
fn eq(&self, other: &T) -> bool {
16+
self.to_string() == other.as_ref()
17+
}
18+
}
19+
20+
impl<Span, Symbol: hash::Hash> hash::Hash for Ident<Span, Symbol> {
21+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
22+
self.sym.hash(state);
23+
self.is_raw.hash(state);
24+
}
25+
}
26+
27+
/// Prints the identifier as a string that should be losslessly convertible back
28+
/// into the same identifier.
29+
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
30+
impl<Span, Symbol: fmt::Display> fmt::Display for Ident<Span, Symbol> {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
if self.is_raw {
33+
f.write_str("r#")?;
34+
}
35+
fmt::Display::fmt(&self.sym, f)
36+
}
37+
}

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: 34 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)]
2020
pub struct Symbol(NonZero<u32>);
2121

2222
impl !Send for Symbol {}
@@ -97,6 +97,38 @@ 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 Hash for Symbol {
115+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
116+
self.0.hash(state);
117+
}
118+
}
119+
120+
impl PartialOrd for Symbol {
121+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
122+
Some(self.cmp(other))
123+
}
124+
}
125+
126+
impl Ord for Symbol {
127+
fn cmp(&self, other: &Self) -> cmp::Ordering {
128+
self.with(|s| other.with(|o| s.cmp(o)))
129+
}
130+
}
131+
100132
impl<S> Encode<S> for Symbol {
101133
fn encode(self, w: &mut Writer, s: &mut S) {
102134
self.with(|sym| sym.encode(w, s))

library/proc_macro/src/lib.rs

Lines changed: 9 additions & 5 deletions
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)]
10221022
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10231023
pub struct Ident(bridge::Ident<bridge::client::Span, bridge::client::Symbol>);
10241024

@@ -1083,10 +1083,7 @@ impl Ident {
10831083
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10841084
impl fmt::Display for Ident {
10851085
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1086-
if self.0.is_raw {
1087-
f.write_str("r#")?;
1088-
}
1089-
fmt::Display::fmt(&self.0.sym, f)
1086+
self.0.fmt(f)
10901087
}
10911088
}
10921089

@@ -1100,6 +1097,13 @@ impl fmt::Debug for Ident {
11001097
}
11011098
}
11021099

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

src/tools/clippy/tests/ui/auxiliary/proc_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl Expander {
465465
} else if let TT::Punct(p) = &input.tt
466466
&& p.as_char() == '!'
467467
&& let TT::Ident(name) = &tt
468-
&& name.to_string() == "inline"
468+
&& *name == "inline"
469469
{
470470
let g = expect_tt(
471471
input.iter.next(),

0 commit comments

Comments
 (0)