#interning #string-interning

tiny-interner

Easy to use string interner with fast access to underlying strings and minimal memory footprint

4 releases

0.1.5 Apr 22, 2023
0.1.3 Apr 22, 2023
0.1.2 Apr 22, 2023
0.1.1 Apr 22, 2023

#56 in #string-interning

34 downloads per month

MIT license

12KB
219 lines

🪐 tiny_interner crate.

~300 lines of Rust code that implement string internering for your programming language compiler.

Example

fn main() {
    let interner = Interner::default();
    let s1 = interner.get_or_intern("test");
    let s2 = interner.get_or_intern("test");
    assert_eq!(s1, s2);

    assert_eq!(interner.resolve(0).unwrap(), "test");
}

lib.rs:

~300 lines of Rust code that implement string internering for your programming language compiler.

The crate caches strings and associates them with unique symbols. These allows constant time comparisons and look-ups to underlying interned strings.

Examples:

Internings:

use tiny_interner::Interner;

let mut interner = Interner::default();
let symbol0 = interner.get_or_intern("A");
let symbol1 = interner.get_or_intern("B");
let symbol2 = interner.get_or_intern("C");
let symbol3 = interner.get_or_intern("A");

assert_ne!(symbol0, symbol1);
assert_ne!(symbol0, symbol2);
assert_ne!(symbol1, symbol2);
assert_eq!(symbol0, symbol3);

Resolving symbols:

use tiny_interner::Interner;

let mut interner = Interner::default();
let symbol0 = interner.get_or_intern("A");
let symbol1 = interner.get_or_intern("B");

assert_eq!(interner.resolve(0), Some("A"));
assert_eq!(interner.resolve(1), Some("B"));
assert_eq!(interner.resolve(2), None);

Dependencies

~1.5MB
~24K SLoC