Crate symboltable

source ·
Expand description

This crate offers a SymbolTable type which can store strings as lightweight [Symbols], which enable extremely fast comparison and total order operations. Depending on the backing data structure, the SymbolTable may also offer fast convertion from Symbol to String. Finally, Symbol is parameterized by a type, allowing you to intern strings coming from incomparable sources without the possibility of mixing them up. For example, if you intern an Address: Into and Username: Into, you can get back a Symbol

and a Symbol. These two Symbol types will share the same store and any benefits of compression, while ensuring you don’t mix up one Symbol for another, as is easy with strings: ```text fn foo(address: String, username: String); foo(my_username, my_address); // This is well-typed, but is logically erronious, because the parameters were mixed up. fn foo2(address: Symbol
, username: Symbol); // This formulation would produce an type error when you accidently // swap the argument positions.

Structs

A Symbol uniquely represents each String contained in the [SymbolTable]. It serves as a lookup key into the table, allowing anyone holding a Symbol to recover the interned value, or to compare the interned value against other interned values of the same type. These comparisons are O(1).
A SymbolTable allows you to store items according to their String representation in a lookup table. The lookup table typically compresses the strings to save space on large tables. The SymbolTable provides a handy, opaque ID for each entry in the table, called a Symbol. This Symbol allows for O(1) comparison of strings because the table is responsible for encoding string uniqueness into each id.
ResolutionErr occurs when a [Symbol] is resolved on a [SymbolTable] from which it did not originate. If a user creates two separate [SymbolTable]s, a [Symbol] from one table is not available to be resolved by the other table. If a user attempts this, a ResolutionErr is returned, indicating that the identities of the two tables are different.

Enums

Traits

A type is Internable if it supports conversion to and from String, and it is static. It doesn’t always need to be parsable from a string, but the output of .toString() must be parsable by TryFrom()
Interner is a backing store for the [SymbolTable]. It is responsible for implementing [Symbol] uniqueness and String compression. You can provide your own interner, or use one of the provided implementations. Most users should expect to use one of the implementations provided by this library. You should only expect to implement Interner yourself if the compression algorithms are not suitable for your needs.