Struct Interner

Source
pub struct Interner { /* private fields */ }
Expand description

Storage for interned strings.

Implementations§

Source§

impl Interner

Source

pub fn new() -> Self

Create a new interner.

Source

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.

Source

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.

Source

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

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

Trait Implementations§

Source§

impl Default for Interner

Source§

fn default() -> Interner

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

impl Index<Istr> for Interner

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, interned: Istr) -> &Self::Output

Performs the indexing (container[index]) operation. 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.