UTF-8 string and bytestring interner and symbol table. Used to implement storage
for the Ruby Symbol
table and the constant name table in Artichoke
Ruby.
Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the
:name
and:"string"
literals syntax, and by the variousto_sym
methods. The sameSymbol
object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name.
Intaglio is a UTF-8 and bytestring interner, which means it stores a single copy
of an immutable &str
or &[u8]
that can be referred to by a stable u32
token.
Interned strings and bytestrings are cheap to compare and copy because they are
represented as a u32
integer.
Intaglio is an alternate name for an engraved gem, a gemstone that has been carved with an image. The Intaglio crate is used to implement an immutable Symbol store in Artichoke Ruby.
Add this to your Cargo.toml
:
toml
[dependencies]
intaglio = "1.0"
Then intern UTF-8 strings like:
rust
fn intern_and_get() -> Result<(), Box<dyn std::error::Error>> {
let mut table = intaglio::SymbolTable::new();
let name: &'static str = "abc";
let sym = table.intern(name)?;
let retrieved = table.get(sym);
assert_eq!(Some(name), retrieved);
assert_eq!(sym, table.intern("abc".to_string())?);
Ok(())
}
Or intern bytestrings like:
rust
fn intern_and_get() -> Result<(), Box<dyn std::error::Error>> {
let mut table = intaglio::bytes::SymbolTable::new();
let name: &'static [u8] = b"abc";
let sym = table.intern(name)?;
let retrieved = table.get(sym);
assert_eq!(Some(name), retrieved);
assert_eq!(sym, table.intern(b"abc".to_vec())?);
Ok(())
}
Intaglio interns owned and borrowed strings with no additional copying by
leveraging Cow
and Box::leak
. This requires unsafe code in the Drop
implementation of SymbolTable
. CI runs drop
tests under Miri.
All features are enabled by default.
Vec<u8>
and &'static [u8]
). Disabling this drops the [bstr
]
dependency.intaglio
is licensed under the MIT License (c) Ryan Lopopolo.