Struct Intern

Source
pub struct Intern<'a> { /* private fields */ }

Implementations§

Source§

impl Intern<'_>

Source

pub fn new() -> Self

Create a new intern table.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new intern table with the given capacity.

Source

pub fn intern<V: Into<String> + AsRef<str>>(&mut self, input: V) -> InternId

Intern a string. Returns the interned id. If the string is already interned, returns the existing id. The string is stored in the intern table for the lifetime of the program. The id is a 32-bit integer, so there can be at most 2^32 unique strings interned. If the limit is reached, this function will panic. The id is guaranteed to be unique for the lifetime of the program.

§Examples
use intern_string::Intern;

let mut intern = Intern::new();
let id = intern.intern("hello");
assert_eq!(intern.lookup(id), "hello");
Source

pub fn lookup(&self, id: InternId) -> &str

Lookup the interned string by id.

§Panics

Panics if the id is not valid.

§Examples
use intern_string::Intern;

let mut intern = Intern::new();
let id = intern.intern("hello");
assert_eq!(intern.lookup(id), "hello");
Source

pub fn try_lookup(&self, id: InternId) -> Option<&str>

Lookup the interned string by id. Returns None if the id is not valid.

§Examples
use intern_string::Intern;

let mut intern = Intern::new();
let id = intern.intern("hello");
assert_eq!(intern.try_lookup(id), Some("hello"));

Trait Implementations§

Source§

impl<'a> Default for Intern<'a>

Source§

fn default() -> Intern<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for Intern<'a>

§

impl<'a> RefUnwindSafe for Intern<'a>

§

impl<'a> Send for Intern<'a>

§

impl<'a> Sync for Intern<'a>

§

impl<'a> Unpin for Intern<'a>

§

impl<'a> UnwindSafe for Intern<'a>

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.