From 661ef626dd8b78a199aa76c58f788349cbbcbe46 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 8 Jun 2026 13:37:40 +0200 Subject: feat(engine-ecs): add support for dyn Any component handles --- engine-ecs/src/entity.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'engine-ecs/src/entity.rs') diff --git a/engine-ecs/src/entity.rs b/engine-ecs/src/entity.rs index f551de1..d7b63be 100644 --- a/engine-ecs/src/entity.rs +++ b/engine-ecs/src/entity.rs @@ -1,4 +1,4 @@ -use std::any::type_name; +use std::any::{type_name, Any}; use std::ops::Deref; use std::sync::LazyLock; @@ -122,6 +122,40 @@ impl<'a> Handle<'a> ) } + /// Returns a reference to the component with the ID `id` in this entity. + /// `None` is returned if the component isn't found. + /// + /// # Panics + /// Will panic if: + /// - The component is borrowed mutably elsewhere + #[must_use] + pub fn get_any_by_id(&self, id: Uid) -> Option> + { + let component = self.get_matching_components(id).next()?; + + Some(ComponentHandle::new_any(&component).unwrap_or_else(|err| { + panic!("Creating handle to component with id {id} failed: {err}"); + })) + } + + /// Returns a reference to the component with the ID `id` in this entity. + /// `None` is returned if the component isn't found. + /// + /// # Panics + /// Will panic if: + /// - The component is borrowed elsewhere + #[must_use] + pub fn get_any_by_id_mut(&self, id: Uid) -> Option> + { + let component = self.get_matching_components(id).next()?; + + Some( + ComponentHandleMut::new_any(&component, self.world).unwrap_or_else(|err| { + panic!("Creating handle to component with id {id} failed: {err}"); + }), + ) + } + /// Returns a mutable reference to the component with the ID `id` in this entity. /// `None` is returned if the component isn't found. /// -- cgit v1.2.3-18-g5258