Struct ResourceRegistry

Source
pub struct ResourceRegistry { /* private fields */ }
Expand description

A type-erased registry for storing arbitrary resources using generational handles.

§Fields

  • next_id: Next available numeric id for newly registered resources.
  • versions: Per-id version counter (generation) for handle validation.
  • stores: Map of TypeId -> boxed Vec<T> type-erased.
  • ids: Map of id -> (TypeId, index in its store).
  • reverse_ids: Reverse map of (TypeId, index) -> id.
  • unique_resources: Map of TypeId -> unique resource instance.

§Safety

  • All type-erased accesses are guarded by TypeId checks and handle version validation to prevent type confusion and use-after-free via stale handles.

Implementations§

Source§

impl ResourceRegistry

Source

pub fn new() -> Self

Creates a new, empty ResourceRegistry.

Source

pub fn register_unique<T: 'static>(&mut self, resource: T)

Registers or overwrites the unique resource of type T.

If a resource of this type already exists, it will be replaced.

§Type Parameters
  • T: The type of the resource. Must be 'static.
§Parameters
  • resource: The resource instance to store as a unique singleton.
Source

pub fn get_unique<T: 'static>(&self) -> Option<&T>

Gets an immutable reference to the unique resource of type T, if it exists.

§Type Parameters
  • T: The type of the resource requested.
§Returns
  • Option<&T>: Some reference to the resource if registered; None if not found.
Source

pub fn get_unique_mut<T: 'static>(&mut self) -> Option<&mut T>

Gets a mutable reference to the unique resource of type T, if it exists.

§Type Parameters
  • T: The type of the resource requested.
§Returns
  • Option<&mut T>: Some mutable reference to the resource if registered; None if not found.
Source

pub fn remove_unique<T: 'static>(&mut self) -> Option<T>

Removes the unique resource of type T from storage and returns it.

§Type Parameters
  • T: The type of the resource to remove.
§Returns
  • Option<T>: The removed resource if it existed; None if no resource of type T was found.
Source

pub fn register_resource<T: 'static>(&mut self, resource: T) -> ResourceHandle

Registers a resource of type T and returns a unique ResourceHandle.

§Type Parameters
  • T: The concrete type of the resource to store. Must be 'static.
§Parameters
  • resource: The resource instance to store in the registry.
§Returns
  • ResourceHandle: A generational handle uniquely identifying the stored resource.
§Notes
  • The handle can later be used to retrieve or remove the resource.
  • Handles are versioned to prevent use-after-free access.
Source

pub fn get_resource<T: 'static>(&self, handle: ResourceHandle) -> Option<&T>

Retrieves an immutable reference to a resource using its handle.

§Type Parameters
  • T: The expected type of the resource to retrieve.
§Parameters
  • handle: A ResourceHandle returned from register_resource.
§Returns
  • Some(&T): A reference to the resource if the handle is valid and the type matches.
  • None: If the handle is invalid, outdated (version mismatch), or of the wrong type.
§Safety
  • Ensures the handle’s version matches the current version of the stored resource.
Source

pub fn get_resource_mut<T: 'static>( &mut self, handle: ResourceHandle, ) -> Option<&mut T>

Retrieves a mutable reference to a resource using its handle.

§Type Parameters
  • T: The expected type of the resource to retrieve.
§Parameters
  • handle: A ResourceHandle returned from register_resource.
§Returns
  • Some(&mut T): A mutable reference to the resource if the handle is valid and type matches.
  • None: If the handle is invalid, outdated, or of the wrong type.
§Safety
  • Only succeeds if the handle is still valid and uniquely identifies a resource of type T.
Source

pub fn remove_resource<T: 'static>( &mut self, handle: ResourceHandle, ) -> Option<T>

Removes a resource from the registry, invalidating its handle.

§Type Parameters
  • T: The expected type of the resource to remove.
§Parameters
  • handle: The ResourceHandle identifying the resource to remove.
§Returns
  • Some(T): The removed resource if the handle was valid and matched the expected type.
  • None: If the handle was invalid, outdated (version mismatch), or mismatched in type.
§Notes
  • This function also increments the resource’s version, invalidating all existing handles.
  • If the removed element is not at the end of its internal storage, the moved resource’s index is updated, and its handle remains valid.

Auto Trait Implementations§

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.