summaryrefslogtreecommitdiff
path: root/ecs/src/entity.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/entity.rs')
-rw-r--r--ecs/src/entity.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs
index 6c9ec32..34fd19f 100644
--- a/ecs/src/entity.rs
+++ b/ecs/src/entity.rs
@@ -69,7 +69,7 @@ impl<'a> Handle<'a>
#[must_use]
pub fn get_mut<ComponentT: Component>(
&self,
- ) -> Option<ComponentHandleMut<'_, ComponentT>>
+ ) -> Option<ComponentHandleMut<'a, ComponentT>>
{
assert_eq!(ComponentT::id().kind(), UidKind::Component);
@@ -87,6 +87,39 @@ impl<'a> Handle<'a>
)
}
+ /// Returns a mutable 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 ID is not a component/pair ID
+ /// - The component is borrowed elsewhere
+ /// - The component type is incorrect
+ #[must_use]
+ pub fn get_with_id_mut<ComponentData: 'static>(
+ &self,
+ id: Uid,
+ ) -> Option<ComponentHandleMut<'a, ComponentData>>
+ {
+ assert!(
+ matches!(id.kind(), UidKind::Component | UidKind::Pair),
+ "ID {id:?} is not a component/pair ID"
+ );
+
+ let component = self.get_matching_components(id).next()?;
+
+ Some(
+ ComponentHandleMut::from_entity_component_ref(&component).unwrap_or_else(
+ |err| {
+ panic!(
+ "Creating handle to component {} failed: {err}",
+ type_name::<ComponentData>()
+ );
+ },
+ ),
+ )
+ }
+
#[inline]
#[must_use]
pub fn get_matching_components(&self, component_uid: Uid)