@@ -1091,6 +1091,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
1091
1091
///
1092
1092
/// # See also
1093
1093
///
1094
+ /// - [`component`](Self::component) a panicking version of this function.
1094
1095
/// - [`get_component_mut`](Self::get_component_mut) to get a mutable reference of a component.
1095
1096
#[ inline]
1096
1097
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> {
1121
1122
1122
1123
/// Returns a mutable reference to the component `T` of the given entity.
1123
1124
///
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.
1125
1126
///
1126
1127
/// # Example
1127
1128
///
@@ -1145,6 +1146,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
1145
1146
///
1146
1147
/// # See also
1147
1148
///
1149
+ /// - [`component_mut`](Self::component_mut) a panicking version of this function.
1148
1150
/// - [`get_component`](Self::get_component) to get a shared reference of a component.
1149
1151
#[ inline]
1150
1152
pub fn get_component_mut < T : Component > (
@@ -1155,6 +1157,54 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
1155
1157
unsafe { self . get_component_unchecked_mut ( entity) }
1156
1158
}
1157
1159
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
+
1158
1208
/// Returns a mutable reference to the component `T` of the given entity.
1159
1209
///
1160
1210
/// In case of a nonexisting entity or mismatched component, a [`QueryComponentError`] is returned instead.
0 commit comments