Skip to content

Commit 20cd383

Browse files
authored
Mention Mut in QueryData docs, clarify behaviour of Mut vs &mut in Mut docs (#19254)
# Objective - Fix #13843 - Clarify the difference between Mut and &mut when accessing query data ## Solution - Mention `Mut` in `QueryData` docs as an example of a type that implements this trait - Give example of `iter_mut` vs `iter` access to `Mut` and `& mut` parameters ## Testing -
1 parent 2bda628 commit 20cd383

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

crates/bevy_ecs/src/change_detection.rs

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -898,63 +898,39 @@ impl_debug!(Ref<'w, T>,);
898898

899899
/// Unique mutable borrow of an entity's component or of a resource.
900900
///
901-
/// This can be used in queries to opt into change detection on both their mutable and immutable forms, as opposed to
902-
/// `&mut T`, which only provides access to change detection while in its mutable form:
901+
/// This can be used in queries to access change detection from immutable query methods, as opposed
902+
/// to `&mut T` which only provides access to change detection from mutable query methods.
903903
///
904904
/// ```rust
905905
/// # use bevy_ecs::prelude::*;
906906
/// # use bevy_ecs::query::QueryData;
907907
/// #
908-
/// #[derive(Component, Clone)]
908+
/// #[derive(Component, Clone, Debug)]
909909
/// struct Name(String);
910910
///
911-
/// #[derive(Component, Clone, Copy)]
911+
/// #[derive(Component, Clone, Copy, Debug)]
912912
/// struct Health(f32);
913913
///
914-
/// #[derive(Component, Clone, Copy)]
915-
/// struct Position {
916-
/// x: f32,
917-
/// y: f32,
918-
/// };
919-
///
920-
/// #[derive(Component, Clone, Copy)]
921-
/// struct Player {
922-
/// id: usize,
923-
/// };
924-
///
925-
/// #[derive(QueryData)]
926-
/// #[query_data(mutable)]
927-
/// struct PlayerQuery {
928-
/// id: &'static Player,
929-
///
930-
/// // Reacting to `PlayerName` changes is expensive, so we need to enable change detection when reading it.
931-
/// name: Mut<'static, Name>,
932-
///
933-
/// health: &'static mut Health,
934-
/// position: &'static mut Position,
935-
/// }
936-
///
937-
/// fn update_player_avatars(players_query: Query<PlayerQuery>) {
938-
/// // The item returned by the iterator is of type `PlayerQueryReadOnlyItem`.
939-
/// for player in players_query.iter() {
940-
/// if player.name.is_changed() {
941-
/// // Update the player's name. This clones a String, and so is more expensive.
942-
/// update_player_name(player.id, player.name.clone());
943-
/// }
944-
///
945-
/// // Update the health bar.
946-
/// update_player_health(player.id, *player.health);
914+
/// fn my_system(mut query: Query<(Mut<Name>, &mut Health)>) {
915+
/// // Mutable access provides change detection information for both parameters:
916+
/// // - `name` has type `Mut<Name>`
917+
/// // - `health` has type `Mut<Health>`
918+
/// for (name, health) in query.iter_mut() {
919+
/// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
920+
/// println!("Health: {:?} (last changed: {:?})", health, health.last_changed());
921+
/// # println!("{}{}", name.0, health.0); // Silence dead_code warning
922+
/// }
947923
///
948-
/// // Update the player's position.
949-
/// update_player_position(player.id, *player.position);
924+
/// // Immutable access only provides change detection for `Name`:
925+
/// // - `name` has type `Ref<Name>`
926+
/// // - `health` has type `&Health`
927+
/// for (name, health) in query.iter() {
928+
/// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
929+
/// println!("Health: {:?}", health);
950930
/// }
951931
/// }
952932
///
953-
/// # bevy_ecs::system::assert_is_system(update_player_avatars);
954-
///
955-
/// # fn update_player_name(player: &Player, new_name: Name) {}
956-
/// # fn update_player_health(player: &Player, new_health: Health) {}
957-
/// # fn update_player_position(player: &Player, new_position: Position) {}
933+
/// # bevy_ecs::system::assert_is_system(my_system);
958934
/// ```
959935
pub struct Mut<'w, T: ?Sized> {
960936
pub(crate) value: &'w mut T,

crates/bevy_ecs/src/query/fetch.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ use variadics_please::all_tuples;
4747
/// - **[`Ref`].**
4848
/// Similar to change detection filters but it is used as a query fetch parameter.
4949
/// It exposes methods to check for changes to the wrapped component.
50+
/// - **[`Mut`].**
51+
/// Mutable component access, with change detection data.
5052
/// - **[`Has`].**
5153
/// Returns a bool indicating whether the entity has the specified component.
5254
///

0 commit comments

Comments
 (0)