Struct intaglio::SymbolTable

source ·
pub struct SymbolTable<S = RandomState> { /* private fields */ }
Expand description

UTF-8 string interner.

This symbol table is implemented by storing UTF-8 strings with a fast path for &str that are already 'static.

See crate documentation for more.

§Usage

let mut table = SymbolTable::new();
let sym = table.intern("abc")?;
assert_eq!(sym, table.intern("abc".to_string())?);
assert!(table.contains(sym));
assert!(table.is_interned("abc"));

Implementations§

source§

impl SymbolTable<RandomState>

source

pub fn new() -> Self

Constructs a new, empty SymbolTable with default capacity.

This function will always allocate. To construct a symbol table without allocating, call SymbolTable::with_capacity(0).

§Examples
let table = SymbolTable::new();
assert_eq!(0, table.len());
assert!(table.capacity() >= 4096);
source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty SymbolTable with the specified capacity.

The symbol table will be able to hold at least capacity strings without reallocating. If capacity is 0, the symbol table will not allocate.

§Examples
let table = SymbolTable::with_capacity(10);
assert_eq!(0, table.len());
assert!(table.capacity() >= 10);
source§

impl<S> SymbolTable<S>

source

pub fn with_hasher(hash_builder: S) -> Self

Constructs a new, empty SymbolTable with default capacity and the given hash builder.

§Examples
let hash_builder = RandomState::new();
let table = SymbolTable::with_hasher(hash_builder);
assert_eq!(0, table.len());
assert!(table.capacity() >= 4096);
source

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

Constructs a new, empty SymbolTable with the specified capacity and the given hash builder.

§Examples
let hash_builder = RandomState::new();
let table = SymbolTable::with_capacity_and_hasher(10, hash_builder);
assert_eq!(0, table.len());
assert!(table.capacity() >= 10);
source

pub fn capacity(&self) -> usize

Returns the number of strings the table can hold without reallocating.

§Examples
let table = SymbolTable::with_capacity(10);
assert!(table.capacity() >= 10);
source

pub fn len(&self) -> usize

Returns the number of interned strings in the table.

§Examples
let mut table = SymbolTable::new();
assert_eq!(0, table.len());

table.intern("abc")?;
// only uniquely interned strings grow the symbol table.
table.intern("abc")?;
table.intern("xyz")?;
assert_eq!(2, table.len());
source

pub fn is_empty(&self) -> bool

Returns true if the symbol table contains no interned strings.

§Examples
let mut table = SymbolTable::new();
assert!(table.is_empty());

table.intern("abc")?;
assert!(!table.is_empty());
source

pub fn contains(&self, id: Symbol) -> bool

Returns true if the symbol table contains the given symbol.

§Examples
let mut table = SymbolTable::new();
assert!(!table.contains(Symbol::new(0)));

let sym = table.intern("abc")?;
assert!(table.contains(Symbol::new(0)));
assert!(table.contains(sym));
source

pub fn get(&self, id: Symbol) -> Option<&str>

Returns a reference to the string associated with the given symbol.

If the given symbol does not exist in the underlying symbol table, None is returned.

The lifetime of the returned reference is bound to the symbol table.

§Examples
let mut table = SymbolTable::new();
assert!(table.get(Symbol::new(0)).is_none());

let sym = table.intern("abc".to_string())?;
assert_eq!(Some("abc"), table.get(Symbol::new(0)));
assert_eq!(Some("abc"), table.get(sym));
source

pub fn iter(&self) -> Iter<'_>

Returns an iterator over all Symbols and strings in the SymbolTable.

§Examples
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let iter = table.iter();
let mut map = HashMap::new();
map.insert(Symbol::new(0), "abc");
map.insert(Symbol::new(1), "xyz");
map.insert(Symbol::new(2), "123");
map.insert(Symbol::new(3), "789");
assert_eq!(map, iter.collect::<HashMap<_, _>>());
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let iter = table.iter();
assert_eq!(table.len(), iter.count());
source

pub fn all_symbols(&self) -> AllSymbols<'_>

Returns an iterator over all Symbols in the SymbolTable.

§Examples
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let mut all_symbols = table.all_symbols();
assert_eq!(Some(Symbol::new(0)), all_symbols.next());
assert_eq!(Some(Symbol::new(1)), all_symbols.nth_back(2));
assert_eq!(None, all_symbols.next());
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let all_symbols = table.all_symbols();
assert_eq!(table.len(), all_symbols.count());
source

pub fn strings(&self) -> Strings<'_>

Returns an iterator over all strings in the SymbolTable.

§Examples
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let mut strings = table.strings();
assert_eq!(Some("abc"), strings.next());
assert_eq!(Some("xyz"), strings.nth_back(2));
assert_eq!(None, strings.next());
let mut table = SymbolTable::new();
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.intern("789")?;

let strings = table.strings();
assert_eq!(table.len(), strings.count());
source§

impl<S> SymbolTable<S>
where S: BuildHasher,

source

pub fn intern<T>(&mut self, contents: T) -> Result<Symbol, SymbolOverflowError>
where T: Into<Cow<'static, str>>,

Intern a string for the lifetime of the symbol table.

The returned Symbol allows retrieving of the underlying string. Equal strings will be inserted into the symbol table exactly once.

This function only allocates if the underlying symbol table has no remaining capacity.

§Errors

If the symbol table would grow larger than u32::MAX interned strings, the Symbol counter would overflow and a SymbolOverflowError is returned.

§Examples
let mut table = SymbolTable::new();
let sym = table.intern("abc".to_string())?;
table.intern("xyz".to_string())?;
table.intern("123")?;
table.intern("789")?;

assert_eq!(4, table.len());
assert_eq!(Some("abc"), table.get(sym));
source

pub fn check_interned(&self, contents: &str) -> Option<Symbol>

Returns the Symbol identifier for contents if it has been interned before, None otherwise.

This method does not modify the symbol table.

§Examples
let mut table = SymbolTable::new();
assert!(!table.is_interned("abc"));
assert_eq!(None, table.check_interned("abc"));

table.intern("abc".to_string())?;
assert!(table.is_interned("abc"));
assert_eq!(Some(Symbol::new(0)), table.check_interned("abc"));
source

pub fn is_interned(&self, contents: &str) -> bool

Returns true if the given string has been interned before.

This method does not modify the symbol table.

§Examples
let mut table = SymbolTable::new();
assert!(!table.is_interned("abc"));
assert_eq!(None, table.check_interned("abc"));

table.intern("abc".to_string())?;
assert!(table.is_interned("abc"));
assert_eq!(Some(Symbol::new(0)), table.check_interned("abc"));
source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given SymbolTable. The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the new capacity overflows usize.

§Examples
let mut table = SymbolTable::with_capacity(1);
table.intern("abc")?;
table.reserve(10);
assert!(table.capacity() >= 11);
source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the symbol table as much as possible.

It will drop down as close as possible to the length but the allocator may still inform the symbol table that there is space for a few more elements.

§Examples
let mut table = SymbolTable::with_capacity(10);
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.shrink_to_fit();
assert!(table.capacity() >= 3);
source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the symbol table with a lower bound.

The capacity will remain at least as large as both the length and the supplied value.

If the current capacity is less than the lower limit, this is a no-op.

§Examples
let mut table = SymbolTable::with_capacity(10);
table.intern("abc")?;
table.intern("xyz")?;
table.intern("123")?;
table.shrink_to(5);
assert!(table.capacity() >= 5);

Trait Implementations§

source§

impl<S: Debug> Debug for SymbolTable<S>

source§

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

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

impl<S: Default> Default for SymbolTable<S>

source§

fn default() -> SymbolTable<S>

Returns the “default value” for a type. Read more
source§

impl<S> Drop for SymbolTable<S>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> IntoIterator for &'a SymbolTable

§

type Item = (Symbol, &'a str)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<S> Freeze for SymbolTable<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for SymbolTable<S>
where S: RefUnwindSafe,

§

impl<S> Send for SymbolTable<S>
where S: Send,

§

impl<S> Sync for SymbolTable<S>
where S: Sync,

§

impl<S> Unpin for SymbolTable<S>
where S: Unpin,

§

impl<S> UnwindSafe for SymbolTable<S>
where S: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.