pub struct KeyedRegistry<K, V> { /* private fields */ }
Expand description
A container that can be used for registering values of a given type and retrieving references by a caller-specified key.
A registry is a centralized container that values can be inserted into and borrowed from. A registry provides several guarantees:
- Arena-based allocated values using an
Arena
(all references are valid for the lifetime of the container). - Runtime-checked immutable and mutable borrow rules.
- Values can be borrowed completely independent of one another.
A single value can be moved into the registry using KeyedRegistry::register
, and multiple
values can be moved in using KeyedRegistry::register_extend
.
Implementations§
Source§impl<K, V> KeyedRegistry<K, V>where
K: Key,
impl<K, V> KeyedRegistry<K, V>where
K: Key,
Sourcepub fn with_capacity(size: usize) -> Self
pub fn with_capacity(size: usize) -> Self
Creates a new registry with the given capacity.
Sourcepub fn register(&self, key: K, value: V) -> bool
pub fn register(&self, key: K, value: V) -> bool
Registers a new value in the arena.
Returns whether or not the value was registered in the registry. If there is already a value associated with the given key, no insertion occurs.
Sourcepub fn register_extend<I>(&self, iterable: I)where
I: IntoIterator<Item = (K, V)>,
pub fn register_extend<I>(&self, iterable: I)where
I: IntoIterator<Item = (K, V)>,
Registers the contents of an iterator in the registry.
Sourcepub fn reserve(&self, additional: usize)
pub fn reserve(&self, additional: usize)
Ensures there is enough continuous space for at least additional
values.
Sourcepub fn into_vec(self) -> Vec<V>
pub fn into_vec(self) -> Vec<V>
Converts the KeyedRegistry<K, V>
into a Vec<V>
.
Keys are completely lost.
Sourcepub fn iter(
&self,
) -> impl Iterator<Item = (&K, Result<ElementRef<'_, V>, BorrowError>)>
pub fn iter( &self, ) -> impl Iterator<Item = (&K, Result<ElementRef<'_, V>, BorrowError>)>
Returns an iterator that provides immutable access to all key-value pairs in the registry.
Sourcepub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (&K, Result<ElementRefMut<'_, V>, BorrowError>)>
pub fn iter_mut( &mut self, ) -> impl Iterator<Item = (&K, Result<ElementRefMut<'_, V>, BorrowError>)>
Returns an iterator that provides mutable access to all key-value pairs in the registry.
Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over all keys in the registry.
Sourcepub fn values(
&self,
) -> impl Iterator<Item = Result<ElementRef<'_, V>, BorrowError>>
pub fn values( &self, ) -> impl Iterator<Item = Result<ElementRef<'_, V>, BorrowError>>
Returns an iterator that provides immutable access to all elements in the registry.
Sourcepub fn values_mut(
&mut self,
) -> impl Iterator<Item = Result<ElementRefMut<'_, V>, BorrowError>>
pub fn values_mut( &mut self, ) -> impl Iterator<Item = Result<ElementRefMut<'_, V>, BorrowError>>
Returns an iterator that provides mutable access to all elements in the registry.
Sourcepub fn get_unchecked<R>(&self, key: &R) -> ElementRef<'_, V>
pub fn get_unchecked<R>(&self, key: &R) -> ElementRef<'_, V>
Returns a reference to a value previously registered in the registry.
Panics if there is a borrow error.
Sourcepub fn get<R>(&self, key: &R) -> Result<ElementRef<'_, V>, BorrowError>
pub fn get<R>(&self, key: &R) -> Result<ElementRef<'_, V>, BorrowError>
Tries to get a reference to a value previously registered in the registry.
Sourcepub fn get_mut_unchecked<R>(&self, key: &R) -> ElementRefMut<'_, V>
pub fn get_mut_unchecked<R>(&self, key: &R) -> ElementRefMut<'_, V>
Returns a mutable reference to a value previously registered in the registry.
Panics if there is a borrow error.
Sourcepub fn get_mut<R>(&self, key: &R) -> Result<ElementRefMut<'_, V>, BorrowError>
pub fn get_mut<R>(&self, key: &R) -> Result<ElementRefMut<'_, V>, BorrowError>
Tries to get a mutable reference to a value previously registered in the registry.
Sourcepub fn contains_key<R>(&self, key: &R) -> bool
pub fn contains_key<R>(&self, key: &R) -> bool
Checks if the registry contains an item associated with the given key.
Sourcepub fn safe_to_drop(&mut self) -> bool
pub fn safe_to_drop(&mut self) -> bool
Checks if the registry is safe to drop.
A registry is safe to drop if all elements are not borrowed. This check is not thread safe.