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 ofTypeId-> boxedVec<T>type-erased.ids: Map of id -> (TypeId, index in its store).reverse_ids: Reverse map of (TypeId, index) -> id.unique_resources: Map ofTypeId-> unique resource instance.
§Safety
- All type-erased accesses are guarded by
TypeIdchecks and handle version validation to prevent type confusion and use-after-free via stale handles.
Implementations§
Source§impl ResourceRegistry
impl ResourceRegistry
Sourcepub fn register_unique<T: 'static>(&mut self, resource: T)
pub fn register_unique<T: 'static>(&mut self, resource: T)
Sourcepub fn get_unique<T: 'static>(&self) -> Option<&T>
pub fn get_unique<T: 'static>(&self) -> Option<&T>
Sourcepub fn get_unique_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_unique_mut<T: 'static>(&mut self) -> Option<&mut T>
Sourcepub fn remove_unique<T: 'static>(&mut self) -> Option<T>
pub fn remove_unique<T: 'static>(&mut self) -> Option<T>
Sourcepub fn register_resource<T: 'static>(&mut self, resource: T) -> ResourceHandle
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.
Sourcepub fn get_resource<T: 'static>(&self, handle: ResourceHandle) -> Option<&T>
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: AResourceHandlereturned fromregister_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.
Sourcepub fn get_resource_mut<T: 'static>(
&mut self,
handle: ResourceHandle,
) -> Option<&mut T>
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: AResourceHandlereturned fromregister_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.
Sourcepub fn remove_resource<T: 'static>(
&mut self,
handle: ResourceHandle,
) -> Option<T>
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: TheResourceHandleidentifying 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§
impl Freeze for ResourceRegistry
impl !RefUnwindSafe for ResourceRegistry
impl !Send for ResourceRegistry
impl !Sync for ResourceRegistry
impl Unpin for ResourceRegistry
impl !UnwindSafe for ResourceRegistry
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
Mutably borrows from an owned value. Read more