pub struct Symbol(/* private fields */);
Expand description
Interned string with very fast comparison and hashing.
Symbols should typically be used as extremely fast program-internal identifiers.
§Comparison
Symbol comparison is just comparison between single pointers, which are guaranteed to be identical for identical strings. No O(n) string comparisons occur.
The implementation of Ord
between symbols also does not perform string
comparison, but rather compares pointer values. However, the
PartialOrd<str>
implementations do compare strings.
§Hashing
The hash value of a symbol is not predictable, as it depends on specific
pointer values, which are determined by both linking order and heap layout
(for dynamically created symbols). In particular, the hash value of Symbol
is not the same as the hash value of the underlying str
.
For this reason, Symbol
does not implement Borrow<str>
, which would
imply that it would hash to the same value as its corresponding string
value. To prevent accidents, Symbol
also does not implement Deref<Target = str>
(this restriction may be lifted in future).
The hash value of symbols may change even between invocations of the same binary, so should not be relied upon in any way.
§Leaks
Once created, symbols are never freed, and there is no way to “garbage-collect” symbols. This means that dynamically creating symbols from user input or runtime data is a great way to create memory leaks. Use only for static or semi-static identifiers, or otherwise trusted input.
Implementations§
Source§impl Symbol
impl Symbol
Sourcepub fn new(string: impl AsRef<str>) -> Symbol
pub fn new(string: impl AsRef<str>) -> Symbol
Create a deduplicated symbol at runtime.
All calls to this function with the same string argument will return a
bit-identical Symbol
.
This function has some overhead, because it needs to take at least a
global read-lock, and potentially a write-lock if the string has not
been seen before. Additionally, opposed to
new_static()
, this function also needs to allocate
a copy of the string on the heap and leak it.
When the string is statically known at compile time, prefer the
sym!(...)
macro. When the string is
statically known to live forever, prefer
new_static()
.
Please note that symbols are never “garbage collected”, so creating an unbounded number of symbols in this way can be considered a memory leak. In particular, creating symbols from untrusted user input is a denial-of-service hazard.
Sourcepub fn new_static(string: &'static &'static str) -> Symbol
pub fn new_static(string: &'static &'static str) -> Symbol
Create a deduplicated symbol at runtime from a static reference to a static string.
If the symbol has not previously been registered, this sidesteps the need to allocate and leak the string. Using this function does not allocate memory, outside of what is needed for registering the symbol for subsequent lookups.
This function has some overhead, because it needs to take at least a global read lock, and potentially a write-lock if the string has not been seen before.
When the string is statically known at compile time, prefer the
sym!(...)
macro.
The use case for this function is the scenario when a string is only known at runtime, but the caller wants to allocate it. For example, the string could be part of a larger (manually leaked) allocation.
Sourcepub fn get(string: impl AsRef<str>) -> Option<Symbol>
pub fn get(string: impl AsRef<str>) -> Option<Symbol>
Get a previously registered symbol.
This returns None
if the string has not previously been registered.
This function has some overhead, because it needs to acquire a global
read-lock, but it is faster than Symbol::new()
and never leaks
memory.
Sourcepub unsafe fn new_unchecked(registered_symbol: &'static &'static str) -> Symbol
pub unsafe fn new_unchecked(registered_symbol: &'static &'static str) -> Symbol
New pre-interned symbol
§Safety
registered_symbol
must be a globally unique string reference (i.e., it
has already been interned through the global registry).
The only valid external usage of this function is to call it with a
value previously returned from Symbol::inner()
.
Sourcepub const fn as_str(&self) -> &'static str
pub const fn as_str(&self) -> &'static str
Get the string representation of this symbol.
This operation is guaranteed to not take any locks, and is effectively free.
Sourcepub const fn inner(&self) -> &'static &'static str
pub const fn inner(&self) -> &'static &'static str
Get the underlying representation of this symbol.
Sourcepub const fn as_ptr(&self) -> NonNull<&'static str>
pub const fn as_ptr(&self) -> NonNull<&'static str>
Get the underlying pointer value of this symbol.
This is the basis for computing equality and hashes. Symbols representing the same string always have the same pointer value.
Trait Implementations§
Source§impl AsRef<Symbol> for StaticSymbol
impl AsRef<Symbol> for StaticSymbol
Source§impl Borrow<Symbol> for StaticSymbol
impl Borrow<Symbol> for StaticSymbol
Source§impl Debug for Symbol
Note: This impl forwards string formatting options to the underlying string.
impl Debug for Symbol
Note: This impl forwards string formatting options to the underlying string.
Source§impl Display for Symbol
Note: This impl forwards string formatting options to the underlying string.
impl Display for Symbol
Note: This impl forwards string formatting options to the underlying string.
Source§impl From<&StaticSymbol> for Symbol
impl From<&StaticSymbol> for Symbol
Source§fn from(value: &StaticSymbol) -> Symbol
fn from(value: &StaticSymbol) -> Symbol
Source§impl From<StaticSymbol> for Symbol
impl From<StaticSymbol> for Symbol
Source§fn from(value: StaticSymbol) -> Symbol
fn from(value: StaticSymbol) -> Symbol
Source§impl Ord for Symbol
impl Ord for Symbol
Source§impl PartialEq<StaticSymbol> for Symbol
impl PartialEq<StaticSymbol> for Symbol
Source§impl PartialEq<Symbol> for StaticSymbol
impl PartialEq<Symbol> for StaticSymbol
Source§impl PartialOrd<&str> for Symbol
impl PartialOrd<&str> for Symbol
Source§impl PartialOrd<Symbol> for &str
impl PartialOrd<Symbol> for &str
Source§impl PartialOrd<Symbol> for str
impl PartialOrd<Symbol> for str
Source§impl PartialOrd<str> for Symbol
impl PartialOrd<str> for Symbol
Source§impl PartialOrd for Symbol
impl PartialOrd for Symbol
impl Copy for Symbol
impl Eq 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§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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.