pub struct Interner { /* private fields */ }
Expand description
Storage for interned strings.
Implementations§
Source§impl Interner
impl Interner
Sourcepub fn intern(&self, key: &str) -> Istr
pub fn intern(&self, key: &str) -> Istr
Intern a string, returning an interned string that it is cheap to copy and perform equality checks on. Strings are only stored in the interner once, no matter how many times they are interned.
let interner = Interner::new();
let hello = interner.intern("hello");
let hello2 = interner.intern("hello");
let world = interner.intern("world");
assert_eq!(hello, hello2);
assert_ne!(hello, world);
§Panics
Returns None
if there are no more available IDs. An interner can store up
to u32::MAX - 1
strings before panicking.
Sourcepub fn try_intern(&self, key: &str) -> Option<Istr>
pub fn try_intern(&self, key: &str) -> Option<Istr>
Like Interner::intern
, but non-panicking in the case that there are no
more available IDs.
Sourcepub fn get_interned(&self, key: &str) -> Option<Istr>
pub fn get_interned(&self, key: &str) -> Option<Istr>
Get an interned string if this string is interned, otherwise return None
.
let interner = Interner::new();
let hello: Istr = interner.intern("hello");
assert_eq!(interner.get_interned("hello"), Some(hello));
assert_eq!(interner.get_interned("world"), None);
Sourcepub fn get_str(&self, interned: Istr) -> Option<&str>
pub fn get_str(&self, interned: Istr) -> Option<&str>
Look up an interned string to get the associated string.
Note that if the interned string was created by another interner
this method will return an arbitrary string or None
.
If you know that an interned string was created by this interner, you can index into
the interner instead. This is the same as calling this method, but panics if the
interned string is not found in this interner instead of returning None
.
let interner = Interner::new();
let interned = interner.intern("hello");
assert_eq!(interner.get_str(interned), Some("hello"));