Skip to content

Commit 0776b37

Browse files
Compare ast::symbol::InternedString by pointer.
1 parent b991723 commit 0776b37

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

src/libsyntax_pos/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(optin_builtin_traits)]
2626
#![allow(unused_attributes)]
2727
#![feature(specialization)]
28+
#![feature(macro_lifetime_matcher)]
2829

2930
use std::borrow::Cow;
3031
use std::cell::Cell;

src/libsyntax_pos/symbol.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use GLOBALS;
1818
use serialize::{Decodable, Decoder, Encodable, Encoder};
1919
use std::collections::HashMap;
2020
use std::fmt;
21+
use std::ops::Deref;
2122

2223
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
2324
pub struct Ident {
@@ -154,9 +155,10 @@ impl Decodable for Symbol {
154155
}
155156
}
156157

157-
impl<T: ::std::ops::Deref<Target=str>> PartialEq<T> for Symbol {
158-
fn eq(&self, other: &T) -> bool {
159-
self.as_str() == other.deref()
158+
impl PartialEq<InternedString> for Symbol {
159+
fn eq(&self, other: &InternedString) -> bool {
160+
// Compare pointers
161+
self.as_str() == *other
160162
}
161163
}
162164

@@ -360,36 +362,49 @@ impl<U: ?Sized> ::std::convert::AsRef<U> for InternedString where str: ::std::co
360362
}
361363
}
362364

363-
impl<T: ::std::ops::Deref<Target = str>> ::std::cmp::PartialEq<T> for InternedString {
364-
fn eq(&self, other: &T) -> bool {
365-
self.string == other.deref()
366-
}
367-
}
368-
369-
impl ::std::cmp::PartialEq<InternedString> for str {
365+
impl ::std::cmp::PartialEq<InternedString> for InternedString {
370366
fn eq(&self, other: &InternedString) -> bool {
371-
self == other.string
367+
self.as_ptr() == other.as_ptr()
372368
}
373369
}
374370

375-
impl<'a> ::std::cmp::PartialEq<InternedString> for &'a str {
376-
fn eq(&self, other: &InternedString) -> bool {
377-
*self == other.string
378-
}
379-
}
371+
macro_rules! impl_partial_eq_for_symbol_and_interned_string {
372+
($(impl $(<$impl_lt:lifetime>)* for $t:ty;)*) => ($(
373+
impl<$($impl_lt),*> ::std::cmp::PartialEq<$t> for InternedString {
374+
fn eq(&self, other: &$t) -> bool {
375+
let s: &str = other.deref();
376+
self.string == s
377+
}
378+
}
380379

381-
impl ::std::cmp::PartialEq<InternedString> for String {
382-
fn eq(&self, other: &InternedString) -> bool {
383-
self == other.string
384-
}
385-
}
380+
impl<$($impl_lt),*> ::std::cmp::PartialEq<InternedString> for $t {
381+
fn eq(&self, other: &InternedString) -> bool {
382+
let s: &str = self.deref();
383+
s == other.string
384+
}
385+
}
386386

387-
impl<'a> ::std::cmp::PartialEq<InternedString> for &'a String {
388-
fn eq(&self, other: &InternedString) -> bool {
389-
*self == other.string
390-
}
387+
impl<$($impl_lt),*> ::std::cmp::PartialEq<$t> for Symbol {
388+
fn eq(&self, other: &$t) -> bool {
389+
self.as_str() == *other
390+
}
391+
}
392+
393+
impl<$($impl_lt),*> ::std::cmp::PartialEq<Symbol> for $t {
394+
fn eq(&self, other: &Symbol) -> bool {
395+
*self == other.as_str()
396+
}
397+
}
398+
)*)
391399
}
392400

401+
impl_partial_eq_for_symbol_and_interned_string!(
402+
impl for str;
403+
impl for String;
404+
impl<'a> for &'a str;
405+
impl<'a> for &'a String;
406+
);
407+
393408
impl !Send for InternedString { }
394409

395410
impl ::std::ops::Deref for InternedString {

0 commit comments

Comments
 (0)