Skip to content

Commit 9fcf862

Browse files
authored
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.
1 parent 2391e44 commit 9fcf862

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

crates/bevy_ecs/src/query/state.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,9 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
11501150
///
11511151
/// This can only be called for read-only queries, see [`par_iter_mut`] for write-queries.
11521152
///
1153+
/// Note that you must use the `for_each` method to iterate over the
1154+
/// results, see [`par_iter_mut`] for an example.
1155+
///
11531156
/// [`par_iter_mut`]: Self::par_iter_mut
11541157
#[inline]
11551158
pub fn par_iter<'w, 's>(
@@ -1170,7 +1173,46 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
11701173
///
11711174
/// This can only be called for mutable queries, see [`par_iter`] for read-only-queries.
11721175
///
1176+
/// # Examples
1177+
///
1178+
/// ```
1179+
/// use bevy_ecs::prelude::*;
1180+
/// use bevy_ecs::query::QueryEntityError;
1181+
///
1182+
/// #[derive(Component, PartialEq, Debug)]
1183+
/// struct A(usize);
1184+
///
1185+
/// # bevy_tasks::ComputeTaskPool::get_or_init(|| bevy_tasks::TaskPool::new());
1186+
///
1187+
/// let mut world = World::new();
1188+
///
1189+
/// # let entities: Vec<Entity> = (0..3).map(|i| world.spawn(A(i)).id()).collect();
1190+
/// # let entities: [Entity; 3] = entities.try_into().unwrap();
1191+
///
1192+
/// let mut query_state = world.query::<&mut A>();
1193+
///
1194+
/// query_state.par_iter_mut(&mut world).for_each(|mut a| {
1195+
/// a.0 += 5;
1196+
/// });
1197+
///
1198+
/// # let component_values = query_state.get_many(&world, entities).unwrap();
1199+
///
1200+
/// # assert_eq!(component_values, [&A(5), &A(6), &A(7)]);
1201+
///
1202+
/// # let wrong_entity = Entity::from_raw(57);
1203+
/// # let invalid_entity = world.spawn_empty().id();
1204+
///
1205+
/// # assert_eq!(query_state.get_many_mut(&mut world, [wrong_entity]).unwrap_err(), QueryEntityError::NoSuchEntity(wrong_entity));
1206+
/// # assert_eq!(query_state.get_many_mut(&mut world, [invalid_entity]).unwrap_err(), QueryEntityError::QueryDoesNotMatch(invalid_entity));
1207+
/// # assert_eq!(query_state.get_many_mut(&mut world, [entities[0], entities[0]]).unwrap_err(), QueryEntityError::AliasedMutability(entities[0]));
1208+
/// ```
1209+
///
1210+
/// # Panics
1211+
/// The [`ComputeTaskPool`] is not initialized. If using this from a query that is being
1212+
/// initialized and run from the ECS scheduler, this should never panic.
1213+
///
11731214
/// [`par_iter`]: Self::par_iter
1215+
/// [`ComputeTaskPool`]: bevy_tasks::ComputeTaskPool
11741216
#[inline]
11751217
pub fn par_iter_mut<'w, 's>(&'s mut self, world: &'w mut World) -> QueryParIter<'w, 's, D, F> {
11761218
self.update_archetypes(world);

crates/bevy_ecs/src/system/query.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
797797
///
798798
/// This can only be called for read-only queries, see [`par_iter_mut`] for write-queries.
799799
///
800+
/// Note that you must use the `for_each` method to iterate over the
801+
/// results, see [`par_iter_mut`] for an example.
802+
///
800803
/// [`par_iter_mut`]: Self::par_iter_mut
801804
/// [`World`]: crate::world::World
802805
#[inline]
@@ -814,6 +817,24 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
814817
///
815818
/// This can only be called for mutable queries, see [`par_iter`] for read-only-queries.
816819
///
820+
/// # Example
821+
///
822+
/// Here, the `gravity_system` updates the `Velocity` component of every entity that contains it:
823+
///
824+
/// ```
825+
/// # use bevy_ecs::prelude::*;
826+
/// #
827+
/// # #[derive(Component)]
828+
/// # struct Velocity { x: f32, y: f32, z: f32 }
829+
/// fn gravity_system(mut query: Query<&mut Velocity>) {
830+
/// const DELTA: f32 = 1.0 / 60.0;
831+
/// query.par_iter_mut().for_each(|mut velocity| {
832+
/// velocity.y -= 9.8 * DELTA;
833+
/// });
834+
/// }
835+
/// # bevy_ecs::system::assert_is_system(gravity_system);
836+
/// ```
837+
///
817838
/// [`par_iter`]: Self::par_iter
818839
/// [`World`]: crate::world::World
819840
#[inline]

0 commit comments

Comments
 (0)