Skip to main content

DirectInternStore

Struct DirectInternStore 

Source
pub struct DirectInternStore<'arena, T> { /* private fields */ }
Expand description

Manages interned &str in the arena allocator. This can also handles alias. Semantics of alias:

  • Alias behaves as the same as canonical. One can call [ensure] for the alias, and the corresponding canonical value is returned.
  • Once registered, it’s impossible to change the value pointing the alias. Caller can’t register alias which is already registered as canonical, or vice versa.

Implementations§

Source§

impl<'arena, T> DirectInternStore<'arena, T>

Source

pub fn new(allocator: &'arena Bump) -> Self

Creates a new instance.

Source§

impl<'arena, T: FromInterned<'arena>> DirectInternStore<'arena, T>

Source

pub fn ensure(&mut self, value: &str) -> T

Interns the given str and returns the shared instance. If value is registered as alias, it’ll resolve to the canonical one. If value is not registered yet, register the value as canonical value.

Source

pub fn try_insert(&mut self, value: &str) -> Result<T, OccupiedError<'arena, T>>

Inserts given value as always canonical. Fails if the given value is already registered.

use bumpalo_intern::direct::{DirectInternStore, OccupiedError, StoredValue};

let arena = bumpalo::Bump::new();
let mut interner = DirectInternStore::new(&arena);
let foo: MyInterned<'_> = interner.try_insert("foo").unwrap();

let err = interner.try_insert("foo").unwrap_err();
assert_eq!(OccupiedError { existing: StoredValue::Canonical(foo) }, err);
Source

pub fn register_alias( &mut self, value: &str, canonical: T, ) -> Result<(), OccupiedError<'arena, T>>

Registers the given value as the alias of canonical. Returns error if given value is already registered as different alias / canonical.

use bumpalo_intern::direct::DirectInternStore;

let arena = bumpalo::Bump::new();
let mut interner = DirectInternStore::new(&arena);

let foo: MyInterned<'_> = interner.ensure(&"foo".to_string());
interner.register_alias("foo_alias", foo).unwrap();
assert_eq!(Some(foo), interner.resolve("foo_alias"));

// idempotent alias is allowed.
interner.register_alias("foo_alias", foo).unwrap();

// different alias is not allowed.
let bar = interner.ensure(&"bar".to_string());
interner.register_alias("foo_alias", bar).unwrap_err();
Source

pub fn resolve(&self, value: &str) -> Option<T>

Returns the specified value if found, otherwise None.

use bumpalo_intern::direct::DirectInternStore;

let arena = bumpalo::Bump::new();
let mut interner = DirectInternStore::new(&arena);
let foo: MyInterned<'_> = interner.ensure("foo");

assert_eq!(Some(foo), interner.resolve("foo"));
assert_eq!(None, interner.resolve("bar"));
Source

pub fn iter<'container>(&'container self) -> Iter<'arena, 'container, T> ⓘ
where 'arena: 'container,

Returns the Iterator for all elements.

use bumpalo_intern::direct::{DirectInternStore, StoredValue};

let arena = bumpalo::Bump::new();
let mut interner = DirectInternStore::new(&arena);
let foo: MyInterned<'_> = interner.ensure("foo");

let got: Vec<StoredValue<MyInterned<'_>>> = interner.iter().collect();
assert_eq!(vec![StoredValue::Canonical(foo)], got);

Auto Trait Implementations§

§

impl<'arena, T> !RefUnwindSafe for DirectInternStore<'arena, T>

§

impl<'arena, T> !Send for DirectInternStore<'arena, T>

§

impl<'arena, T> !Sync for DirectInternStore<'arena, T>

§

impl<'arena, T> !UnwindSafe for DirectInternStore<'arena, T>

§

impl<'arena, T> Forget for DirectInternStore<'arena, T>

§

impl<'arena, T> Freeze for DirectInternStore<'arena, T>
where *const u8 is !null: Freeze, PhantomData<(&'arena str, Option<InternedStr<'arena>>)>: Freeze, &'arena Bump: Freeze, PhantomData<T>: Freeze,

§

impl<'arena, T> Move for DirectInternStore<'arena, T>

§

impl<'arena, T> Unpin for DirectInternStore<'arena, T>
where T: Unpin, *const u8 is !null: Unpin, &'arena str: Unpin, &'arena Bump: Unpin,

§

impl<'arena, T> UnsafeUnpin for DirectInternStore<'arena, T>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.