Struct Registry

Source
pub struct Registry<T> { /* private fields */ }
Expand description

A container that can be used for registering values of a given type and retrieving references by handle.

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 Registry::register, and multiple values can be moved in using Registry::register_extend.

Implementations§

Source§

impl<T> Registry<T>

Source

pub fn new() -> Self

Creates a new registry.

Source

pub fn with_capacity(size: usize) -> Self

Creates a new registry with the given capacity.

Source

pub fn is_empty(&self) -> bool

Checks if the registry is empty.

Source

pub fn len(&self) -> usize

Returns the number of elements owned by the registry.

Source

pub fn register(&self, value: T) -> Handle

Registers a new value in the arena, returning a handle to that value.

Source

pub fn register_extend<I>(&self, iterable: I) -> (Handle, Handle)
where I: IntoIterator<Item = T>,

Registers the contents of an iterator in the registry.

Returns a numeric range of handles [a, b), where a is the handle of the first element inserted and b is the handle after the last element inserted.

Source

pub fn reserve(&self, additional: usize)

Ensures there is enough continuous space for at least additional values.

Source

pub fn into_vec(self) -> Vec<T>

Converts the Registry<T> into a Vec<T>.

Items will maintain their handle as their vector index.

Source

pub fn iter( &self, ) -> impl Iterator<Item = Result<ElementRef<'_, T>, BorrowError>>

Returns an iterator that provides immutable access to all elements in the registry, in order of registration.

Source

pub fn iter_mut( &mut self, ) -> impl Iterator<Item = Result<ElementRefMut<'_, T>, BorrowError>>

Returns an iterator that provides mutable access to all elements in the registry, in order of registration.

Source

pub fn get_unchecked(&self, handle: Handle) -> ElementRef<'_, T>

Returns a reference to a value previously registered in the registry.

Panics if there is a borrow error.

Source

pub fn get(&self, handle: Handle) -> Result<ElementRef<'_, T>, BorrowError>

Tries to get a reference to a value previously registered in the registry.

Source

pub fn get_mut_unchecked(&self, handle: Handle) -> ElementRefMut<'_, T>

Returns a mutable reference to a value previously registered in the registry.

Panics if there is a borrow error.

Source

pub fn get_mut( &self, handle: Handle, ) -> Result<ElementRefMut<'_, T>, BorrowError>

Tries to get a mutable reference to a value previously registered in the registry.

Source

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.

Trait Implementations§

Source§

impl<T: Default> Default for Registry<T>

Source§

fn default() -> Registry<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Registry<T>

§

impl<T> !RefUnwindSafe for Registry<T>

§

impl<T> Send for Registry<T>
where T: Send,

§

impl<T> !Sync for Registry<T>

§

impl<T> Unpin for Registry<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Registry<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 = 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.