#interning #string-interning

stagiaire

Yet another string interner

3 releases (breaking)

0.3.0 Dec 29, 2022
0.2.1 Dec 29, 2022
0.1.0 Dec 28, 2022

#46 in #interning

MIT license

7KB
98 lines

Stagiaire

This Rust crate provides a string interner.

Please refer to crate documentation for details.

There are already several similar crates but I've been using this one in various personal projects and I needed an excuse to learn how to publish to crates.io.


lib.rs:

A string interner.

A string interner stores a pool of immutable strings keeping a single copy of each string value. A Symbol is a wrapper over a pointer to one of these unique string values. Symbols can be compared quickly (pointer rather than string comparisons) and are cheaper to store than strings when several occurrences of a given string exist.

Examples

use stagiaire::Symbol;

// Create a new symbol.
let a_foo = Symbol::new("foo");
assert_eq!(a_foo.as_str(), "foo");

// Create another symbol that refers to an existing value.
let another_foo = Symbol::new("foo");
assert_eq!(a_foo, another_foo);

// Both symbols point to the same underlying value.
assert_eq!(a_foo.as_str().as_ptr(), another_foo.as_str().as_ptr());

// A symbol has the same size as a reference.
assert_eq!(std::mem::size_of::<Symbol>(), std::mem::size_of::<&str>());

// Symbols pointing to different values are not equal.
let a_bar = Symbol::new("bar");
assert_ne!(a_bar, a_foo);

Lifetime

The interner is a process-wide singleton not exposed programmatically and string values stored there persist until the owning process terminates and have therefore a 'static lifetime.

Thread-safety

Symbol values can be created and accessed from multiple threads.

Serialization

A Symbol can optionally be serialized and deserialized using serde. To enable this, build with the serde feature on.

Dependencies

~170KB