diff options
| author | HampusM <hampus@hampusmat.com> | 2026-06-08 13:37:40 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-06-08 13:37:40 +0200 |
| commit | 661ef626dd8b78a199aa76c58f788349cbbcbe46 (patch) | |
| tree | 85e6b3ee0316c85166b1f211a26560f40964cb76 /engine-ecs/src/entity.rs | |
| parent | 04bde94d9088f5a7c2a885d812495fa173cd67c0 (diff) | |
feat(engine-ecs): add support for dyn Any component handles
Diffstat (limited to 'engine-ecs/src/entity.rs')
| -rw-r--r-- | engine-ecs/src/entity.rs | 36 |
1 files changed, 35 insertions, 1 deletions
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<ComponentHandle<'a, dyn Any>> + { + 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<ComponentHandleMut<'a, dyn Any>> + { + 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. /// |
