Skip to main content

Interner

Struct Interner 

Source
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

Source

pub fn new() -> Self

Creates an empty interner that performs no allocation up front.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty interner with storage pre-reserved for capacity distinct strings.

Source

pub fn len(&self) -> usize

Number of distinct strings currently interned.

Source

pub fn is_empty(&self) -> bool

Returns true if the interner holds no strings.

Source

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.

Source

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.

Source

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"));
Source

pub fn contains(&self, s: &str) -> bool

Returns true if s has already been interned.

Equivalent to Interner::lookup returning Some.

Source

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");
Source

pub fn iter(&self) -> Iter<'_>

Iterator over (Symbol, &str) pairs for every interned string, in insertion order.

Trait Implementations§

Source§

impl Debug for Interner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Interner

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.