Skip to content

Commit

Permalink
Rename LocalInternedString as SymbolStr.
Browse files Browse the repository at this point in the history
It makes the relationship with `Symbol` clearer. The `Str` suffix
matches the existing `Symbol::as_str()` method nicely, and is also
consistent with it being a wrapper of `&str`.
  • Loading branch information
nnethercote committed Nov 1, 2019
1 parent 87cbf0a commit 1b154a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/librustc/ich/impls_syntax.rs
Expand Up @@ -9,7 +9,7 @@ use std::mem;
use syntax::ast;
use syntax::feature_gate;
use syntax::parse::token;
use syntax::symbol::LocalInternedString;
use syntax::symbol::SymbolStr;
use syntax::tokenstream;
use syntax_pos::SourceFile;

Expand All @@ -18,21 +18,21 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
use smallvec::SmallVec;
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};

impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
impl<'a> HashStable<StableHashingContext<'a>> for SymbolStr {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let str = self as &str;
str.hash_stable(hcx, hasher)
}
}

impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
type KeyType = LocalInternedString;
impl<'a> ToStableHashKey<StableHashingContext<'a>> for SymbolStr {
type KeyType = SymbolStr;

#[inline]
fn to_stable_hash_key(&self,
_: &StableHashingContext<'a>)
-> LocalInternedString {
-> SymbolStr {
self.clone()
}
}
Expand All @@ -45,12 +45,12 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
}

impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
type KeyType = LocalInternedString;
type KeyType = SymbolStr;

#[inline]
fn to_stable_hash_key(&self,
_: &StableHashingContext<'a>)
-> LocalInternedString {
-> SymbolStr {
self.as_str()
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/libsyntax_pos/symbol.rs
Expand Up @@ -806,9 +806,9 @@ impl Ident {
Ident::new(self.name, self.span.modern_and_legacy())
}

/// Convert the name to a `LocalInternedString`. This is a slowish
/// operation because it requires locking the symbol interner.
pub fn as_str(self) -> LocalInternedString {
/// Convert the name to a `SymbolStr`. This is a slowish operation because
/// it requires locking the symbol interner.
pub fn as_str(self) -> SymbolStr {
self.name.as_str()
}
}
Expand Down Expand Up @@ -896,11 +896,11 @@ impl Symbol {
})
}

/// Convert to a `LocalInternedString`. This is a slowish operation because
/// it requires locking the symbol interner.
pub fn as_str(self) -> LocalInternedString {
/// Convert to a `SymbolStr`. This is a slowish operation because it
/// requires locking the symbol interner.
pub fn as_str(self) -> SymbolStr {
with_interner(|interner| unsafe {
LocalInternedString {
SymbolStr {
string: std::mem::transmute::<&str, &str>(interner.get(self))
}
})
Expand Down Expand Up @@ -973,6 +973,7 @@ impl Interner {
self.names.insert(string, name);
name
}

// Get the symbol as a string. `Symbol::as_str()` should be used in
// preference to this function.
pub fn get(&self, symbol: Symbol) -> &str {
Expand Down Expand Up @@ -1092,15 +1093,14 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
/// safely treat `string` which points to interner data, as an immortal string,
/// as long as this type never crosses between threads.
//
// FIXME: ensure that the interner outlives any thread which uses
// `LocalInternedString`, by creating a new thread right after constructing the
// interner.
// FIXME: ensure that the interner outlives any thread which uses `SymbolStr`,
// by creating a new thread right after constructing the interner.
#[derive(Clone, Eq, PartialOrd, Ord)]
pub struct LocalInternedString {
pub struct SymbolStr {
string: &'static str,
}

impl<U: ?Sized> std::convert::AsRef<U> for LocalInternedString
impl<U: ?Sized> std::convert::AsRef<U> for SymbolStr
where
str: std::convert::AsRef<U>
{
Expand All @@ -1110,28 +1110,28 @@ where
}
}

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

impl !Send for LocalInternedString {}
impl !Sync for LocalInternedString {}
impl !Send for SymbolStr {}
impl !Sync for SymbolStr {}

impl std::ops::Deref for LocalInternedString {
impl std::ops::Deref for SymbolStr {
type Target = str;
#[inline]
fn deref(&self) -> &str { self.string }
}

impl fmt::Debug for LocalInternedString {
impl fmt::Debug for SymbolStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.string, f)
}
}

impl fmt::Display for LocalInternedString {
impl fmt::Display for SymbolStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.string, f)
}
Expand Down

0 comments on commit 1b154a3

Please sign in to comment.