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 data source that values can be inserted into. After insertion, the registry returns a Handle, which will refer to the same value for the lifetime of the registry.

A single value can be moved into the registry using Registry::register, and multiple values can be moved in using Registry::register_extend.

Internally, a registry uses an Arena for allocating values, which guarantees references are valid for the lifetime of the arena. A Registry adds the additional guarantees that all handles will refer to a single value in the underlying arena for the lifetime of the registry.

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 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_mut(&mut self) -> IterMut<'_, T>

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

Registries only allow mutable iteration because the entire registry must be borrowed for the duration of the iteration. The mutable borrow to call this method allows Rust’s borrow checker to enforce this rule.

source

pub fn get_mut(&self, handle: Handle) -> Option<&mut T>

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

source

pub fn get(&self, handle: Handle) -> Option<&T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Registry<T>

§

impl<T> !Send for Registry<T>

§

impl<T> !Sync for Registry<T>

§

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

§

impl<T> UnwindSafe for Registry<T>where T: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.