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>
impl<'arena, T> DirectInternStore<'arena, T>
Source§impl<'arena, T: FromInterned<'arena>> DirectInternStore<'arena, T>
impl<'arena, T: FromInterned<'arena>> DirectInternStore<'arena, T>
Sourcepub fn ensure(&mut self, value: &str) -> T
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.
Sourcepub fn try_insert(&mut self, value: &str) -> Result<T, OccupiedError<'arena, T>>
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);Sourcepub fn register_alias(
&mut self,
value: &str,
canonical: T,
) -> Result<(), OccupiedError<'arena, T>>
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();Sourcepub fn resolve(&self, value: &str) -> Option<T>
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"));Sourcepub fn iter<'container>(&'container self) -> Iter<'arena, 'container, T> ⓘwhere
'arena: 'container,
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>
impl<'arena, T> UnsafeUnpin for DirectInternStore<'arena, T>where
*const u8 is !null: UnsafeUnpin,
PhantomData<(&'arena str, Option<InternedStr<'arena>>)>: UnsafeUnpin,
&'arena Bump: UnsafeUnpin,
PhantomData<T>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more