diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-11-06 07:03:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-06 07:03:01 +0100 |
| commit | a0b4b4dafaef944df28f2d9d5e94d6ce190a0504 (patch) | |
| tree | be238e0b515a44f52a5ee8873ffdab38a0560336 /src/libsyntax_pos | |
| parent | e4931eaaa3d95189b30e90d3af9f0db17c41bbb0 (diff) | |
| parent | d0db29003975d8c4b3a552ff8c3a68435173cdc7 (diff) | |
| download | rust-a0b4b4dafaef944df28f2d9d5e94d6ce190a0504.tar.gz rust-a0b4b4dafaef944df28f2d9d5e94d6ce190a0504.zip | |
Rollup merge of #65776 - nnethercote:rename-LocalInternedString-and-more, r=estebank
Rename `LocalInternedString` and more This PR renames `LocalInternedString` as `SymbolStr`, removes an unnecessary `impl` from it, improves comments, and cleans up some `SymbolStr` uses. r? @estebank
Diffstat (limited to 'src/libsyntax_pos')
| -rw-r--r-- | src/libsyntax_pos/symbol.rs | 52 |
1 files changed, 24 insertions, 28 deletions
diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 57131ffe18c..3f7b3e5b3d8 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -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() } } @@ -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)) } }) @@ -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 { @@ -1078,7 +1079,6 @@ impl Ident { } } -// If an interner exists, return it. Otherwise, prepare a fresh one. #[inline] fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T { GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock())) @@ -1092,46 +1092,42 @@ 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 -where - str: std::convert::AsRef<U> -{ - #[inline] - fn as_ref(&self) -> &U { - self.string.as_ref() - } -} - -impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for LocalInternedString { +// This impl allows a `SymbolStr` to be directly equated with a `String` or +// `&str`. +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 { +/// This impl means that if `ss` is a `SymbolStr`: +/// - `*ss` is a `str`; +/// - `&*ss` is a `&str`; +/// - `&ss as &str` is a `&str`, which means that `&ss` can be passed to a +/// function expecting a `&str`. +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) } |
