Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add panicking helpers for getting components from Query #9659

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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