pub struct Interner { /* private fields */ }Expand description
String interner. See the module-level docs.
§Examples
use arena_lib::Interner;
let mut interner = Interner::new();
let a = interner.intern("user:42");
let b = interner.intern("user:42");
let c = interner.intern("user:7");
assert_eq!(a, b);
assert_ne!(a, c);
assert_eq!(interner.resolve(a), Some("user:42"));
assert_eq!(interner.len(), 2);Implementations§
Source§impl Interner
impl Interner
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty interner with storage pre-reserved for capacity
distinct strings.
Sourcepub fn intern(&mut self, s: &str) -> Symbol
pub fn intern(&mut self, s: &str) -> Symbol
Interns s and returns its Symbol.
Idempotent: repeated calls with the same input return the same
symbol. Panics if the symbol counter would overflow u32::MAX
— use Interner::try_intern for the explicit fallible variant.
Sourcepub fn try_intern(&mut self, s: &str) -> Result<Symbol>
pub fn try_intern(&mut self, s: &str) -> Result<Symbol>
Interns s, returning a Symbol on success or
Error::CounterOverflow if the interner cannot represent more
distinct strings.
Sourcepub fn resolve(&self, symbol: Symbol) -> Option<&str>
pub fn resolve(&self, symbol: Symbol) -> Option<&str>
Returns the original string for symbol, or None if the
symbol’s id is out of range for this interner.
Passing a symbol issued by a different interner is undefined
behaviour at the API contract level: the call may return None
(if the foreign id is past this interner’s length) or it may
return some unrelated string (if the foreign id collides with an
in-range local id). Treat Symbol values as opaque handles
scoped to the Interner that issued them.
§Examples
use arena_lib::Interner;
let mut interner = Interner::new();
let sym = interner.intern("payload");
assert_eq!(interner.resolve(sym), Some("payload"));Sourcepub fn contains(&self, s: &str) -> bool
pub fn contains(&self, s: &str) -> bool
Returns true if s has already been interned.
Equivalent to Interner::lookup returning Some.
Sourcepub fn lookup(&self, s: &str) -> Option<Symbol>
pub fn lookup(&self, s: &str) -> Option<Symbol>
Returns the symbol previously assigned to s, without inserting
a new entry if the string is unknown.
Use this when you want to probe the interner without growing it — for example, to enforce that only pre-registered identifiers are accepted.
§Examples
use arena_lib::Interner;
let mut interner = Interner::new();
let known = interner.intern("known");
assert_eq!(interner.lookup("known"), Some(known));
assert_eq!(interner.lookup("unknown"), None);
assert_eq!(interner.len(), 1, "lookup must not insert");