Crate intern_all
source ·Expand description
A type-agnostic interner
Can be used to deduplicate various structures for fast equality comparisons. The design favours flexibility over efficiency, ideally you can throw any type into Interner and it’ll just work. The main interface is Interner and Tok, TypedInterner is provided to cover less likely use cases.
use std::env;
use std::path::PathBuf;
use intern_all::{Interner, Tok};
let i = Interner::new();
// Intern a value
let a: Tok<String> = i.i("foo");
// Intern a path
let b: Tok<PathBuf> = i.i(&env::current_dir().unwrap());Some convenience methods are also provided to make working with lists easier
use intern_all::{Interner, Tok};
let i = Interner::new();
// Intern a list as a slice of tokens
let v1: Tok<Vec<Tok<String>>> =
i.i(&[i.i("bar"), i.i("quz"), i.i("quux")][..]);
// Intern a list of internable values
let v2: Tok<Vec<Tok<String>>> =
i.iv(["bar".to_string(), "quz".to_string(), "quux".to_string()]);
// Intern a list of the borrowed form of internable values
let v3: Tok<Vec<Tok<String>>> = i.ibv(["bar", "quz", "quux"]);Structs
- A collection of interners based on their type. Can be used to intern any object that implements ToOwned. Objects of the same type are stored together in a TypedInterner
- A number representing an object of type
Tstored in some interner. Equality comparison costs two pointer comparisons. Ordering is by pointer value. - An interner for any type that implements Borrow. Not many optimizations are employed and the interner uses the default allocator. This and the use of weak references means that a long-lived instance can be kept around with regular calls to TypedInterner::sweep.