Skip to content

Commit 9309d89

Browse files
authored
Add panicking helpers for getting components from Query (#9659)
# Objective - Currently we don't have panicking alternative for getting components from `Query` like for resources. Partially addresses #9443. ## Solution - Add these functions. --- ## Changelog ### Added - `Query::component` and `Query::component_mut` to get specific component from query and panic on error.
1 parent a166b65 commit 9309d89

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

crates/bevy_ecs/src/system/query.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
10911091
///
10921092
/// # See also
10931093
///
1094+
/// - [`component`](Self::component) a panicking version of this function.
10941095
/// - [`get_component_mut`](Self::get_component_mut) to get a mutable reference of a component.
10951096
#[inline]
10961097
pub fn get_component<T: Component>(&self, entity: Entity) -> Result<&T, QueryComponentError> {
@@ -1121,7 +1122,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
11211122

11221123
/// Returns a mutable reference to the component `T` of the given entity.
11231124
///
1124-
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.
1125+
/// In case of a nonexisting entity, mismatched component or missing write acess, a [`QueryComponentError`] is returned instead.
11251126
///
11261127
/// # Example
11271128
///
@@ -1145,6 +1146,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
11451146
///
11461147
/// # See also
11471148
///
1149+
/// - [`component_mut`](Self::component_mut) a panicking version of this function.
11481150
/// - [`get_component`](Self::get_component) to get a shared reference of a component.
11491151
#[inline]
11501152
pub fn get_component_mut<T: Component>(
@@ -1155,6 +1157,54 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
11551157
unsafe { self.get_component_unchecked_mut(entity) }
11561158
}
11571159

1160+
/// Returns a shared reference to the component `T` of the given [`Entity`].
1161+
///
1162+
/// # Panics
1163+
///
1164+
/// Panics in case of a nonexisting entity or mismatched component.
1165+
///
1166+
/// # See also
1167+
///
1168+
/// - [`get_component`](Self::get_component) a non-panicking version of this function.
1169+
/// - [`component_mut`](Self::component_mut) to get a mutable reference of a component.
1170+
#[inline]
1171+
#[track_caller]
1172+
pub fn component<T: Component>(&self, entity: Entity) -> &T {
1173+
match self.get_component(entity) {
1174+
Ok(component) => component,
1175+
Err(error) => {
1176+
panic!(
1177+
"Cannot get component `{:?}` from {entity:?}: {error}",
1178+
TypeId::of::<T>()
1179+
)
1180+
}
1181+
}
1182+
}
1183+
1184+
/// Returns a mutable reference to the component `T` of the given entity.
1185+
///
1186+
/// # Panics
1187+
///
1188+
/// Panics in case of a nonexisting entity, mismatched component or missing write access.
1189+
///
1190+
/// # See also
1191+
///
1192+
/// - [`get_component_mut`](Self::get_component_mut) a non-panicking version of this function.
1193+
/// - [`component`](Self::component) to get a shared reference of a component.
1194+
#[inline]
1195+
#[track_caller]
1196+
pub fn component_mut<T: Component>(&mut self, entity: Entity) -> Mut<'_, T> {
1197+
match self.get_component_mut(entity) {
1198+
Ok(component) => component,
1199+
Err(error) => {
1200+
panic!(
1201+
"Cannot get component `{:?}` from {entity:?}: {error}",
1202+
TypeId::of::<T>()
1203+
)
1204+
}
1205+
}
1206+
}
1207+
11581208
/// Returns a mutable reference to the component `T` of the given entity.
11591209
///
11601210
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.

0 commit comments

Comments
 (0)