Struct Interner

Source
pub struct Interner<I = u32> { /* private fields */ }
Expand description

A struct responsible for efficiently interning strings

§Examples

use mini_intern::Interner;
const TEST_CASE: &str = "test_case";
let mut interner = <Interner<u32>>::with_capacity(64);
_ = interner.intern("something");
_ = interner.intern("something_else");
_ = interner.intern("something_else_again");
_ = interner.intern(TEST_CASE);
assert_eq!(interner.intern(TEST_CASE), 3);

Implementations§

Source§

impl<I> Interner<I>
where I: From<u32> + Copy, u32: From<I>,

Source

pub fn with_capacity(cap: usize) -> Self

Creates an Interner with the specified capacity in memory. Useful for pre-allocating space if the size of the items to be immediately interned is known.

§Examples
use mini_intern::Interner;
let interner: Interner = Interner::with_capacity(32);
Source

pub fn intern(&mut self, name: &str) -> I

Interns the given value. If the value already exists in the interner, its ID is returned and no allocation is performed. If it does not exist, the value is interned and a new ID is created for it.

If the intern space is below the maximum capacity, no allocation occurs.

§Examples
use mini_intern::Interner;
let mut interner: Interner<u32> = Interner::with_capacity(32);
interner.intern("hello");
interner.intern("and");
interner.intern("good");
interner.intern("morning");
assert_eq!(interner.intern("good"), 2u32);
Source

pub fn lookup(&self, id: I) -> &'static str

Trait Implementations§

Source§

impl<I: Debug> Debug for Interner<I>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<I> Freeze for Interner<I>

§

impl<I> RefUnwindSafe for Interner<I>
where I: RefUnwindSafe,

§

impl<I> Send for Interner<I>
where I: Send,

§

impl<I> Sync for Interner<I>
where I: Sync,

§

impl<I> Unpin for Interner<I>
where I: Unpin,

§

impl<I> UnwindSafe for Interner<I>
where I: UnwindSafe,

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.