Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use InternedString instead of Symbol for type parameter types #49266

Closed
Closed
1 change: 1 addition & 0 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#![feature(optin_builtin_traits)]
#![allow(unused_attributes)]
#![feature(specialization)]
#![feature(macro_lifetime_matcher)]

use std::borrow::Cow;
use std::cell::Cell;
Expand Down
65 changes: 40 additions & 25 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use GLOBALS;
use serialize::{Decodable, Decoder, Encodable, Encoder};
use std::collections::HashMap;
use std::fmt;
use std::ops::Deref;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason for this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just efficiency, I guess?)


#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Ident {
Expand Down Expand Up @@ -154,9 +155,10 @@ impl Decodable for Symbol {
}
}

impl<T: ::std::ops::Deref<Target=str>> PartialEq<T> for Symbol {
fn eq(&self, other: &T) -> bool {
self.as_str() == other.deref()
impl PartialEq<InternedString> for Symbol {
fn eq(&self, other: &InternedString) -> bool {
// Compare pointers
self.as_str() == *other
}
}

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

impl<T: ::std::ops::Deref<Target = str>> ::std::cmp::PartialEq<T> for InternedString {
fn eq(&self, other: &T) -> bool {
self.string == other.deref()
}
}

impl ::std::cmp::PartialEq<InternedString> for str {
impl ::std::cmp::PartialEq<InternedString> for InternedString {
fn eq(&self, other: &InternedString) -> bool {
self == other.string
self.as_ptr() == other.as_ptr()
}
}

impl<'a> ::std::cmp::PartialEq<InternedString> for &'a str {
fn eq(&self, other: &InternedString) -> bool {
*self == other.string
}
}
macro_rules! impl_partial_eq_for_symbol_and_interned_string {
($(impl $(<$impl_lt:lifetime>)* for $t:ty;)*) => ($(
impl<$($impl_lt),*> ::std::cmp::PartialEq<$t> for InternedString {
fn eq(&self, other: &$t) -> bool {
let s: &str = other.deref();
self.string == s
}
}

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

impl<'a> ::std::cmp::PartialEq<InternedString> for &'a String {
fn eq(&self, other: &InternedString) -> bool {
*self == other.string
}
impl<$($impl_lt),*> ::std::cmp::PartialEq<$t> for Symbol {
fn eq(&self, other: &$t) -> bool {
self.as_str() == *other
}
}

impl<$($impl_lt),*> ::std::cmp::PartialEq<Symbol> for $t {
fn eq(&self, other: &Symbol) -> bool {
*self == other.as_str()
}
}
)*)
}

impl_partial_eq_for_symbol_and_interned_string!(
impl for str;
impl for String;
impl<'a> for &'a str;
impl<'a> for &'a String;
);

impl !Send for InternedString { }

impl ::std::ops::Deref for InternedString {
Expand Down