Module intaglio::cstr

source ·
Available on crate feature cstr only.
Expand description

Intern C strings.

This module provides a nearly identical API to the one found in the top-level of this crate. There is one important difference:

  1. Interned contents are &CStr instead of &str. Additionally, CString is used where String would have been used.

§Example: intern C string

let mut table = SymbolTable::new();
let sym = table.intern(CStr::from_bytes_with_nul(b"abc\0")?)?;
assert_eq!(sym, table.intern(CString::new(*b"abc")?)?);
assert_eq!(Some(&b"abc\0"[..]), table.get(sym).map(CStr::to_bytes_with_nul));

§Example: symbol iterators

let mut table = SymbolTable::new();
let sym = table.intern(CStr::from_bytes_with_nul(b"abc\0")?)?;
// Retrieve set of `Symbol`s.
let all_symbols = table.all_symbols();
assert_eq!(vec![sym], all_symbols.collect::<Vec<_>>());

table.intern(CStr::from_bytes_with_nul(b"xyz\0")?)?;
let mut map = HashMap::new();
map.insert(Symbol::new(0), CStr::from_bytes_with_nul(b"abc\0")?);
map.insert(Symbol::new(1), CStr::from_bytes_with_nul(b"xyz\0")?);
// Retrieve symbol to C string content mappings.
let iter = table.iter();
assert_eq!(map, iter.collect::<HashMap<_, _>>());

§Performance

In general, one should expect this crate’s performance on &CStr to be roughly similar to performance on &str.

Structs§