Crate internment
source · [−]Expand description
A very easy to use library for interning strings or other data in rust. Interned data is very efficient to either hash or compare for equality (just a pointer comparison). Data is also automatically de-duplicated.
You have three options with the internment crate:
-
Intern
, which will never free your data. This means that anIntern
isCopy
, so you can make as many copies of the pointer as you may care to at no cost. -
LocalIntern
, which will only free your data when the calling thread exits. This means that aLocalIntern
isCopy
, so you can make as many copies of the pointer as you may care to at no cost. However, you cannot share aLocalIntern
with another thread. On the plus side, it is faster to create aLocalIntern
than anIntern
. -
ArcIntern
, which reference-counts your data and frees it when there are no more references.ArcIntern
will keep memory use down, but uses an atomic increment/decrement whenever a clone of your pointer is made, or a pointer is dropped.
In each case, accessing your data is a single pointer dereference, and
the size of any internment data structure (Intern
, LocalIntern
, or
ArcIntern
) is a single pointer. In each case, you have a guarantee
that a single data value (as defined by Eq
and Hash
) will
correspond to a single pointer value. This means that we can use
pointer comparison (and a pointer hash) in place of value comparisons,
which is very fast.
Example
use internment::Intern;
let x = Intern::new("hello");
let y = Intern::new("world");
assert_ne!(x, y);
Structs
A pointer to a reference-counted interned object.
A pointer to an interned object that has been leaked and may be used in any thread without locking.
A pointer to a thread-local interned object.