From 9fcf8621146b7ae0b9f24c984f9718258880e752 Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Sat, 27 Jan 2024 20:13:03 -0600 Subject: [PATCH] bevy_ecs: Add doc example for par_iter_mut (#11311) (#11499) # Objective Fixes #11311 ## Solution Adds an example to the documentation for `par_iter_mut`. I didn't add any examples to `par_iter`, because I couldn't think of a good example and I figure users can infer that `par_iter` and `par_iter_mut` are similar. --- crates/bevy_ecs/src/query/state.rs | 42 +++++++++++++++++++++++++++++ crates/bevy_ecs/src/system/query.rs | 21 +++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 58f5db9a1c1a8..666549d52a6f3 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -1150,6 +1150,9 @@ impl QueryState { /// /// This can only be called for read-only queries, see [`par_iter_mut`] for write-queries. /// + /// Note that you must use the `for_each` method to iterate over the + /// results, see [`par_iter_mut`] for an example. + /// /// [`par_iter_mut`]: Self::par_iter_mut #[inline] pub fn par_iter<'w, 's>( @@ -1170,7 +1173,46 @@ impl QueryState { /// /// This can only be called for mutable queries, see [`par_iter`] for read-only-queries. /// + /// # Examples + /// + /// ``` + /// use bevy_ecs::prelude::*; + /// use bevy_ecs::query::QueryEntityError; + /// + /// #[derive(Component, PartialEq, Debug)] + /// struct A(usize); + /// + /// # bevy_tasks::ComputeTaskPool::get_or_init(|| bevy_tasks::TaskPool::new()); + /// + /// let mut world = World::new(); + /// + /// # let entities: Vec = (0..3).map(|i| world.spawn(A(i)).id()).collect(); + /// # let entities: [Entity; 3] = entities.try_into().unwrap(); + /// + /// let mut query_state = world.query::<&mut A>(); + /// + /// query_state.par_iter_mut(&mut world).for_each(|mut a| { + /// a.0 += 5; + /// }); + /// + /// # let component_values = query_state.get_many(&world, entities).unwrap(); + /// + /// # assert_eq!(component_values, [&A(5), &A(6), &A(7)]); + /// + /// # let wrong_entity = Entity::from_raw(57); + /// # let invalid_entity = world.spawn_empty().id(); + /// + /// # assert_eq!(query_state.get_many_mut(&mut world, [wrong_entity]).unwrap_err(), QueryEntityError::NoSuchEntity(wrong_entity)); + /// # assert_eq!(query_state.get_many_mut(&mut world, [invalid_entity]).unwrap_err(), QueryEntityError::QueryDoesNotMatch(invalid_entity)); + /// # assert_eq!(query_state.get_many_mut(&mut world, [entities[0], entities[0]]).unwrap_err(), QueryEntityError::AliasedMutability(entities[0])); + /// ``` + /// + /// # Panics + /// The [`ComputeTaskPool`] is not initialized. If using this from a query that is being + /// initialized and run from the ECS scheduler, this should never panic. + /// /// [`par_iter`]: Self::par_iter + /// [`ComputeTaskPool`]: bevy_tasks::ComputeTaskPool #[inline] pub fn par_iter_mut<'w, 's>(&'s mut self, world: &'w mut World) -> QueryParIter<'w, 's, D, F> { self.update_archetypes(world); diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 9f0eaf34d4a50..7b7c5ebbc9e08 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -797,6 +797,9 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// This can only be called for read-only queries, see [`par_iter_mut`] for write-queries. /// + /// Note that you must use the `for_each` method to iterate over the + /// results, see [`par_iter_mut`] for an example. + /// /// [`par_iter_mut`]: Self::par_iter_mut /// [`World`]: crate::world::World #[inline] @@ -814,6 +817,24 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// This can only be called for mutable queries, see [`par_iter`] for read-only-queries. /// + /// # Example + /// + /// Here, the `gravity_system` updates the `Velocity` component of every entity that contains it: + /// + /// ``` + /// # use bevy_ecs::prelude::*; + /// # + /// # #[derive(Component)] + /// # struct Velocity { x: f32, y: f32, z: f32 } + /// fn gravity_system(mut query: Query<&mut Velocity>) { + /// const DELTA: f32 = 1.0 / 60.0; + /// query.par_iter_mut().for_each(|mut velocity| { + /// velocity.y -= 9.8 * DELTA; + /// }); + /// } + /// # bevy_ecs::system::assert_is_system(gravity_system); + /// ``` + /// /// [`par_iter`]: Self::par_iter /// [`World`]: crate::world::World #[inline]