Struct Symbol

Source
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

Source

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.

Source

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.

Source

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.

Source

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().

Source

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.

Source

pub const fn inner(&self) -> &'static &'static str

Get the underlying representation of this symbol.

Source

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.

Source

pub fn to_ffi(&self) -> u64

Convert the symbol to an FFI-friendly u64.

Source

pub unsafe fn from_ffi(value: u64) -> Symbol

Reconstitute a symbol from a value previously produced by to_ffi().

§Safety

value must be produced from a previous call to to_ffi() in the current process, and by the exact same version of this crate.

In effect, this function can only be used for roundtrips through foreign code.

Source

pub fn try_from_ffi(value: u64) -> Option<Symbol>

Reconstitute a symbol from a value previously produced by to_ffi(), checking if it is valid.

This involves taking a global read-lock to determine the validity of value.

Source

pub const fn len(&self) -> usize

Length of the underlying string.

Source

pub const fn is_empty(&self) -> bool

Whether or not this is the empty symbol.

Trait Implementations§

Source§

impl AsRef<Symbol> for StaticSymbol

Source§

fn as_ref(&self) -> &Symbol

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for Symbol

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<Symbol> for StaticSymbol

Source§

fn borrow(&self) -> &Symbol

Immutably borrows from an owned value. Read more
Source§

impl Clone for Symbol

Source§

fn clone(&self) -> Symbol

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Symbol

Note: This impl forwards string formatting options to the underlying string.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Symbol

Note: This impl forwards string formatting options to the underlying string.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&StaticSymbol> for Symbol

Source§

fn from(value: &StaticSymbol) -> Symbol

Converts to this type from the input type.
Source§

impl From<&str> for Symbol

Source§

fn from(value: &str) -> Symbol

Converts to this type from the input type.
Source§

impl<'a> From<Cow<'a, str>> for Symbol

Source§

fn from(value: Cow<'a, str>) -> Symbol

Converts to this type from the input type.
Source§

impl From<StaticSymbol> for Symbol

Source§

fn from(value: StaticSymbol) -> Symbol

Converts to this type from the input type.
Source§

impl From<String> for Symbol

Source§

fn from(value: String) -> Symbol

Converts to this type from the input type.
Source§

impl Hash for Symbol

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Symbol

Source§

fn cmp(&self, other: &Symbol) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<&str> for Symbol

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<StaticSymbol> for Symbol

Source§

fn eq(&self, other: &StaticSymbol) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Symbol> for &str

Source§

fn eq(&self, other: &Symbol) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Symbol> for StaticSymbol

Source§

fn eq(&self, other: &Symbol) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Symbol> for str

Source§

fn eq(&self, other: &Symbol) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for Symbol

Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Symbol

Source§

fn eq(&self, other: &Symbol) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd<&str> for Symbol

Source§

fn partial_cmp(&self, other: &&str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd<Symbol> for &str

Source§

fn partial_cmp(&self, other: &Symbol) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd<Symbol> for str

Source§

fn partial_cmp(&self, other: &Symbol) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd<str> for Symbol

Source§

fn partial_cmp(&self, other: &str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd for Symbol

Source§

fn partial_cmp(&self, other: &Symbol) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Symbol

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.