pub struct Symbol(SymbolIndex);
Expand description
An interned string.
Internally, a Symbol
is implemented as an index, and all operations
(including hashing, equality, and ordering) operate on that index. The use
of rustc_index::newtype_index!
means that Option<Symbol>
only takes up 4 bytes,
because rustc_index::newtype_index!
reserves the last 256 values for tagging purposes.
Note that Symbol
cannot directly be a rustc_index::newtype_index!
because it
implements fmt::Debug
, Encodable
, and Decodable
in special ways.
Tuple Fields§
§0: SymbolIndex
Implementations§
source§impl Symbol
impl Symbol
const fn new(n: u32) -> Self
sourcepub fn new_from_decoded(n: u32) -> Self
pub fn new_from_decoded(n: u32) -> Self
for use in Decoder only
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Access the underlying string. This is a slowish operation because it requires locking the symbol interner.
Note that the lifetime of the return value is a lie. It’s not the same
as &self
, but actually tied to the lifetime of the underlying
interner. Interners are long-lived, and there are very few of them, and
this function is typically used for short-lived things, so in practice
it works out ok.
pub fn as_u32(self) -> u32
pub fn is_empty(self) -> bool
sourcepub fn to_ident_string(self) -> String
pub fn to_ident_string(self) -> String
This method is supposed to be used in error messages, so it’s expected to be
identical to printing the original identifier token written in source code
(token_to_string
, Ident::to_string
), except that symbols don’t keep the rawness flag
or edition, so we have to guess the rawness using the global edition.
source§impl Symbol
impl Symbol
fn is_special(self) -> bool
fn is_used_keyword_always(self) -> bool
fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool
fn is_unused_keyword_always(self) -> bool
fn is_unused_keyword_conditional( self, edition: impl Copy + FnOnce() -> Edition, ) -> bool
pub fn is_reserved(self, edition: impl Copy + FnOnce() -> Edition) -> bool
sourcepub fn is_path_segment_keyword(self) -> bool
pub fn is_path_segment_keyword(self) -> bool
A keyword or reserved identifier that can be used as a path segment.
sourcepub fn is_bool_lit(self) -> bool
pub fn is_bool_lit(self) -> bool
Returns true
if the symbol is true
or false
.
sourcepub fn can_be_raw(self) -> bool
pub fn can_be_raw(self) -> bool
Returns true
if this symbol can be a raw identifier.
sourcepub fn is_preinterned(self) -> bool
pub fn is_preinterned(self) -> bool
Is this symbol was interned in compiler’s symbols!
macro
Trait Implementations§
source§impl<CTX> HashStable<CTX> for Symbol
impl<CTX> HashStable<CTX> for Symbol
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher)
source§impl Ord for Symbol
impl Ord for Symbol
source§impl PartialOrd for Symbol
impl PartialOrd for Symbol
source§impl StableCompare for Symbol
impl StableCompare for Symbol
const CAN_USE_UNSTABLE_SORT: bool = true
fn stable_cmp(&self, other: &Self) -> Ordering
source§impl<CTX> ToStableHashKey<CTX> for Symbol
impl<CTX> ToStableHashKey<CTX> for Symbol
impl Copy for Symbol
impl Eq for Symbol
impl StructuralPartialEq for Symbol
Auto Trait Implementations§
impl Freeze for Symbol
impl RefUnwindSafe for Symbol
impl Send for Symbol
impl Sync for Symbol
impl Unpin for Symbol
impl UnwindSafe for Symbol
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
impl<'a, T> Captures<'a> for Twhere
T: ?Sized,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 4 bytes