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.
This commit is contained in:
Hennadii Chernyshchyk 2023-09-04 15:31:12 +03:00 committed by GitHub
parent a166b65241
commit 9309d89bb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1091,6 +1091,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// ///
/// # See also /// # See also
/// ///
/// - [`component`](Self::component) a panicking version of this function.
/// - [`get_component_mut`](Self::get_component_mut) to get a mutable reference of a component. /// - [`get_component_mut`](Self::get_component_mut) to get a mutable reference of a component.
#[inline] #[inline]
pub fn get_component<T: Component>(&self, entity: Entity) -> Result<&T, QueryComponentError> { 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> {
/// Returns a mutable reference to the component `T` of the given entity. /// Returns a mutable reference to the component `T` of the given entity.
/// ///
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead. /// In case of a nonexisting entity, mismatched component or missing write acess, a [`QueryComponentError`] is returned instead.
/// ///
/// # Example /// # Example
/// ///
@ -1145,6 +1146,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// ///
/// # See also /// # See also
/// ///
/// - [`component_mut`](Self::component_mut) a panicking version of this function.
/// - [`get_component`](Self::get_component) to get a shared reference of a component. /// - [`get_component`](Self::get_component) to get a shared reference of a component.
#[inline] #[inline]
pub fn get_component_mut<T: Component>( pub fn get_component_mut<T: Component>(
@ -1155,6 +1157,54 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
unsafe { self.get_component_unchecked_mut(entity) } unsafe { self.get_component_unchecked_mut(entity) }
} }
/// Returns a shared reference to the component `T` of the given [`Entity`].
///
/// # Panics
///
/// Panics in case of a nonexisting entity or mismatched component.
///
/// # See also
///
/// - [`get_component`](Self::get_component) a non-panicking version of this function.
/// - [`component_mut`](Self::component_mut) to get a mutable reference of a component.
#[inline]
#[track_caller]
pub fn component<T: Component>(&self, entity: Entity) -> &T {
match self.get_component(entity) {
Ok(component) => component,
Err(error) => {
panic!(
"Cannot get component `{:?}` from {entity:?}: {error}",
TypeId::of::<T>()
)
}
}
}
/// Returns a mutable reference to the component `T` of the given entity.
///
/// # Panics
///
/// Panics in case of a nonexisting entity, mismatched component or missing write access.
///
/// # See also
///
/// - [`get_component_mut`](Self::get_component_mut) a non-panicking version of this function.
/// - [`component`](Self::component) to get a shared reference of a component.
#[inline]
#[track_caller]
pub fn component_mut<T: Component>(&mut self, entity: Entity) -> Mut<'_, T> {
match self.get_component_mut(entity) {
Ok(component) => component,
Err(error) => {
panic!(
"Cannot get component `{:?}` from {entity:?}: {error}",
TypeId::of::<T>()
)
}
}
}
/// Returns a mutable reference to the component `T` of the given entity. /// Returns a mutable reference to the component `T` of the given entity.
/// ///
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead. /// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.