pub trait Lookup {
// Required methods
fn get(&self, s: &str) -> Option<Symbol>;
fn resolve_with<R, F>(&self, symbol: Symbol, f: F) -> Option<R>
where F: FnOnce(&str) -> R;
fn len(&self) -> usize;
// Provided method
fn is_empty(&self) -> bool { ... }
}Expand description
The read-side contract every interner in the crate implements.
Lookup is the seam that lets generic code accept either the single-threaded
Interner or the
ConcurrentInterner: both look a string up to its
symbol, resolve a symbol back to its bytes, and report their size — all through
&self. Interning is deliberately not on this trait, because the two
interners disagree on how it is called (&mut self for the single-threaded
one, &self for the concurrent one), and forcing a common signature would tax
the single-threaded hot path with synchronisation it does not need.
Resolution is expressed as resolve_with — a closure
that runs against the borrowed string — rather than a method returning
&str. A concurrent interner can only expose the bytes while it holds its
read lock, so the borrow cannot outlive the call; a closure keeps resolution
zero-copy for both interners without leaking the lock’s lifetime into the
signature.
§Examples
use intern_lang::{Interner, Lookup};
fn name_len(interner: &impl Lookup, s: &str) -> Option<usize> {
let sym = interner.get(s)?;
interner.resolve_with(sym, str::len)
}
let mut interner = Interner::new();
let _ = interner.intern("identifier");
assert_eq!(name_len(&interner, "identifier"), Some(10));
assert_eq!(name_len(&interner, "missing"), None);Required Methods§
Sourcefn get(&self, s: &str) -> Option<Symbol>
fn get(&self, s: &str) -> Option<Symbol>
Returns the symbol for s if it has already been interned, without
interning it.
Sourcefn resolve_with<R, F>(&self, symbol: Symbol, f: F) -> Option<R>
fn resolve_with<R, F>(&self, symbol: Symbol, f: F) -> Option<R>
Runs f against the string symbol names, returning its result, or None
if symbol is out of range for this interner.
The borrow passed to f is valid only for the duration of the call. This
keeps resolution zero-copy across both the single-threaded and concurrent
interners; the concurrent one holds its read lock for exactly the span of
f.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".