pub struct UniqueArena<T> { /* private fields */ }
Expand description
An arena whose elements are guaranteed to be unique.
A UniqueArena
holds a set of unique values of type T
, each with an
associated Span
. Inserting a value returns a Handle<T>
, which can be
used to index the UniqueArena
and obtain shared access to the T
element.
Access via a Handle
is an array lookup - no hash lookup is necessary.
The element type must implement Eq
and Hash
. Insertions of equivalent
elements, according to Eq
, all return the same Handle
.
Once inserted, elements may not be mutated.
UniqueArena
is similar to Arena
: If Arena
is vector-like,
UniqueArena
is HashSet
-like.
Implementations§
Source§impl<T> UniqueArena<T>
impl<T> UniqueArena<T>
Source§impl<T: Eq + Hash> UniqueArena<T>
impl<T: Eq + Hash> UniqueArena<T>
Sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)>
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Handle<T>, &T)>
Returns an iterator over the items stored in this arena, returning both the item’s handle and a reference to it.
Sourcepub fn insert(&mut self, value: T, span: Span) -> Handle<T>
pub fn insert(&mut self, value: T, span: Span) -> Handle<T>
Insert a new value into the arena.
Return a Handle<T>
, which can be used to index this arena to get a
shared reference to the element.
If this arena already contains an element that is Eq
to value
,
return a Handle
to the existing element, and drop value
.
If value
is inserted into the arena, associate span
with
it. An element’s span can be retrieved with the get_span
method.
Sourcepub fn replace(&mut self, old: Handle<T>, new: T)
pub fn replace(&mut self, old: Handle<T>, new: T)
Replace an old value with a new value.
§Panics
- if the old value is not in the arena
- if the new value already exists in the arena
Sourcepub fn get(&self, value: &T) -> Option<Handle<T>>
pub fn get(&self, value: &T) -> Option<Handle<T>>
Return this arena’s handle for value
, if present.
If this arena already contains an element equal to value
,
return its handle. Otherwise, return None
.
Sourcepub fn get_handle(&self, handle: Handle<T>) -> Result<&T, BadHandle>
pub fn get_handle(&self, handle: Handle<T>) -> Result<&T, BadHandle>
Return this arena’s value at handle
, if that is a valid handle.
Sourcepub fn check_contains_handle(&self, handle: Handle<T>) -> Result<(), BadHandle>
pub fn check_contains_handle(&self, handle: Handle<T>) -> Result<(), BadHandle>
Assert that handle
is valid for this arena.
Trait Implementations§
Source§impl<T: Clone> Clone for UniqueArena<T>
impl<T: Clone> Clone for UniqueArena<T>
Source§fn clone(&self) -> UniqueArena<T>
fn clone(&self) -> UniqueArena<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> Default for UniqueArena<T>
impl<T> Default for UniqueArena<T>
Auto Trait Implementations§
impl<T> Freeze for UniqueArena<T>
impl<T> RefUnwindSafe for UniqueArena<T>where
T: RefUnwindSafe,
impl<T> Send for UniqueArena<T>where
T: Send,
impl<T> Sync for UniqueArena<T>where
T: Sync,
impl<T> Unpin for UniqueArena<T>where
T: Unpin,
impl<T> UnwindSafe for UniqueArena<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)