Skip to content

Commit

Permalink
Add panicking helpers for getting components from Query (#9659)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
Shatur authored Sep 4, 2023
1 parent a166b65 commit 9309d89
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
///
/// # 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.
#[inline]
pub fn get_component<T: Component>(&self, entity: Entity) -> Result<&T, QueryComponentError> {
Expand Down Expand Up @@ -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.
///
/// 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
///
Expand All @@ -1145,6 +1146,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
///
/// # 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.
#[inline]
pub fn get_component_mut<T: Component>(
Expand All @@ -1155,6 +1157,54 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
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.
///
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.
Expand Down

0 comments on commit 9309d89

Please sign in to comment.