Mention Mut in QueryData docs, clarify behaviour of Mut vs &mut in Mut docs (#19254)

# Objective

- Fix https://github.com/bevyengine/bevy/issues/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

-
This commit is contained in:
theotherphil 2025-06-09 20:21:04 +01:00 committed by GitHub
parent 2bda628ecf
commit 20cd383b31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 44 deletions

View File

@ -898,63 +898,39 @@ impl_debug!(Ref<'w, T>,);
/// Unique mutable borrow of an entity's component or of a resource. /// Unique mutable borrow of an entity's component or of a resource.
/// ///
/// This can be used in queries to opt into change detection on both their mutable and immutable forms, as opposed to /// This can be used in queries to access change detection from immutable query methods, as opposed
/// `&mut T`, which only provides access to change detection while in its mutable form: /// to `&mut T` which only provides access to change detection from mutable query methods.
/// ///
/// ```rust /// ```rust
/// # use bevy_ecs::prelude::*; /// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::query::QueryData; /// # use bevy_ecs::query::QueryData;
/// # /// #
/// #[derive(Component, Clone)] /// #[derive(Component, Clone, Debug)]
/// struct Name(String); /// struct Name(String);
/// ///
/// #[derive(Component, Clone, Copy)] /// #[derive(Component, Clone, Copy, Debug)]
/// struct Health(f32); /// struct Health(f32);
/// ///
/// #[derive(Component, Clone, Copy)] /// fn my_system(mut query: Query<(Mut<Name>, &mut Health)>) {
/// struct Position { /// // Mutable access provides change detection information for both parameters:
/// x: f32, /// // - `name` has type `Mut<Name>`
/// y: f32, /// // - `health` has type `Mut<Health>`
/// }; /// for (name, health) in query.iter_mut() {
/// /// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
/// #[derive(Component, Clone, Copy)] /// println!("Health: {:?} (last changed: {:?})", health, health.last_changed());
/// struct Player { /// # println!("{}{}", name.0, health.0); // Silence dead_code warning
/// id: usize,
/// };
///
/// #[derive(QueryData)]
/// #[query_data(mutable)]
/// struct PlayerQuery {
/// id: &'static Player,
///
/// // Reacting to `PlayerName` changes is expensive, so we need to enable change detection when reading it.
/// name: Mut<'static, Name>,
///
/// health: &'static mut Health,
/// position: &'static mut Position,
/// } /// }
/// ///
/// fn update_player_avatars(players_query: Query<PlayerQuery>) { /// // Immutable access only provides change detection for `Name`:
/// // The item returned by the iterator is of type `PlayerQueryReadOnlyItem`. /// // - `name` has type `Ref<Name>`
/// for player in players_query.iter() { /// // - `health` has type `&Health`
/// if player.name.is_changed() { /// for (name, health) in query.iter() {
/// // Update the player's name. This clones a String, and so is more expensive. /// println!("Name: {:?} (last changed {:?})", name, name.last_changed());
/// update_player_name(player.id, player.name.clone()); /// println!("Health: {:?}", health);
/// }
///
/// // Update the health bar.
/// update_player_health(player.id, *player.health);
///
/// // Update the player's position.
/// update_player_position(player.id, *player.position);
/// } /// }
/// } /// }
/// ///
/// # bevy_ecs::system::assert_is_system(update_player_avatars); /// # bevy_ecs::system::assert_is_system(my_system);
///
/// # fn update_player_name(player: &Player, new_name: Name) {}
/// # fn update_player_health(player: &Player, new_health: Health) {}
/// # fn update_player_position(player: &Player, new_position: Position) {}
/// ``` /// ```
pub struct Mut<'w, T: ?Sized> { pub struct Mut<'w, T: ?Sized> {
pub(crate) value: &'w mut T, pub(crate) value: &'w mut T,

View File

@ -47,6 +47,8 @@ use variadics_please::all_tuples;
/// - **[`Ref`].** /// - **[`Ref`].**
/// Similar to change detection filters but it is used as a query fetch parameter. /// Similar to change detection filters but it is used as a query fetch parameter.
/// It exposes methods to check for changes to the wrapped component. /// It exposes methods to check for changes to the wrapped component.
/// - **[`Mut`].**
/// Mutable component access, with change detection data.
/// - **[`Has`].** /// - **[`Has`].**
/// Returns a bool indicating whether the entity has the specified component. /// Returns a bool indicating whether the entity has the specified component.
/// ///