Module intaglio::bytes

source ·
Available on crate feature bytes only.
Expand description

Intern arbitrary bytes.

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 &[u8] instead of &str. Additionally, Vec<u8> is used where String would have been used.

§Example: intern byte string

let mut table = SymbolTable::new();
let sym = table.intern(&b"abc"[..])?;
assert_eq!(sym, table.intern(b"abc".to_vec())?);
assert_eq!(Some(&b"abc"[..]), table.get(sym));

§Example: symbol iterators

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

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

§Performance

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

Structs§