A string interner.
A string interner is a data structure commonly used in compilers and other contexts that need to cheaply store and compare many often identical strings. "Interning" a string returns an ID (or in many implementations, a pointer) that is cheap to copy and to perform equality checks on. This is achieved by deduplicating strings using an internal hash table.
This string interner also stores all strings in a single bump-allocated buffer, courtesy of [bumpalo
],
avoiding excessive allocation.
I decided to represent interned strings with a 32-bit ID instead of a reference to avoid introducing lifetimes. This does mean that accessing the underlying string requires calling a method on the interner, but this is a simple array-lookup.
Example
use Interner;
let interner = new;
let hello = interner.intern;
let hello2 = interner.intern;
let world = interner.intern;
// Interned strings can be compared cheaply.
assert_ne!;
assert_ne!;
// Getting the associated string for an interned string.
assert_eq!;