pub struct Symbol(/* private fields */);Expand description
Identifier bound to an interned byte string.
A Symbol allows retrieving a reference to the original interned
byte string. Equivalent Symbols will resolve to an identical byte string.
Symbols are based on a u32 index. They are cheap to compare and cheap to
copy.
Symbols are not constrained to the interner which created them.
Implementations§
Source§impl Symbol
impl Symbol
Sourcepub const fn new(id: u32) -> Self
pub const fn new(id: u32) -> Self
Construct a new Symbol from the given u32.
Symbols constructed manually may fail to resolve to an underlying
byte string.
Symbols are not constrained to the interner which created them.
No runtime checks ensure that the underlying interner is called with a
Symbol that the interner itself issued.
§Examples
let sym = Symbol::new(263);
assert_eq!(sym.id(), 263);Sourcepub const fn id(self) -> u32
pub const fn id(self) -> u32
Return the u32 identifier from this Symbol.
§Examples
let sym = Symbol::new(263);
assert_eq!(sym.id(), 263);
assert_eq!(u32::from(sym), 263);Sourcepub fn is_empty<T, U>(self, interner: &T) -> bool
Available on crate feature artichoke only.
pub fn is_empty<T, U>(self, interner: &T) -> bool
artichoke only.Returns whether the symbol is the empty byte slice b"" in the
underlying interner.
If there symbol does not exist in the underlying interner or there is an
error looking up the symbol in the underlying interner, true is
returned.
Sourcepub fn len<T, U>(self, interner: &T) -> usize
Available on crate feature artichoke only.
pub fn len<T, U>(self, interner: &T) -> usize
artichoke only.Returns the length of the byte slice associated with the symbol in the underlying interner.
If there symbol does not exist in the underlying interner or there is an
error looking up the symbol in the underlying interner, 0 is returned.
Sourcepub fn bytes<T, U>(self, interner: &T) -> &[u8] ⓘ
Available on crate feature artichoke only.
pub fn bytes<T, U>(self, interner: &T) -> &[u8] ⓘ
artichoke only.Returns the interned byte slice associated with the symbol in the underlying interner.
If there symbol does not exist in the underlying interner or there is an
error looking up the symbol in the underlying interner, &[] is
returned.
Sourcepub fn inspect<T, U>(self, interner: &T) -> Inspect<'_> ⓘ
Available on crate feature artichoke only.
pub fn inspect<T, U>(self, interner: &T) -> Inspect<'_> ⓘ
artichoke only.Returns an iterator that yields a debug representation of the interned byte slice associated with the symbol in the underlying interner.
This iterator produces char sequences like :spinoso and
:"invalid-\xFF-utf8".
This function can be used to implement the Ruby method
Symbol#inspect.
If the symbol does not exist in the underlying interner or there is an error looking up the symbol in the underlying interner, a default iterator is returned.