Skip to content

Commit f38a6e6

Browse files
committed
Document QueryState (#2298)
# Objective - QueryState is lacking documentation. Fixes #2090 ## Solution - Provide documentation that mirrors Query (as suggested in #2090) and modify as needed. Co-authored-by: James Leflang <[email protected]>
1 parent b47217b commit f38a6e6

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

crates/bevy_ecs/src/query/state.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use bevy_tasks::TaskPool;
1313
use fixedbitset::FixedBitSet;
1414
use thiserror::Error;
1515

16+
/// Provides scoped access to a [`World`] state according to a given [`WorldQuery`] and query filter.
1617
pub struct QueryState<Q: WorldQuery, F: WorldQuery = ()>
1718
where
1819
F::Fetch: FilterFetch,
@@ -35,6 +36,7 @@ impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F>
3536
where
3637
F::Fetch: FilterFetch,
3738
{
39+
/// Creates a new [`QueryState`] from a given [`World`] and inherits the result of `world.id()`.
3840
pub fn new(world: &mut World) -> Self {
3941
let fetch_state = <Q::State as FetchState>::init(world);
4042
let filter_state = <F::State as FetchState>::init(world);
@@ -68,6 +70,7 @@ where
6870
state
6971
}
7072

73+
/// Checks if the query is empty for the given [`World`], where the last change and current tick are given.
7174
#[inline]
7275
pub fn is_empty(&self, world: &World, last_change_tick: u32, change_tick: u32) -> bool {
7376
// SAFE: the iterator is instantly consumed via `none_remaining` and the implementation of
@@ -78,6 +81,12 @@ where
7881
}
7982
}
8083

84+
/// Takes a query for the given [`World`], checks if the given world is the same as the query, and
85+
/// generates new archetypes for the given world.
86+
///
87+
/// # Panics
88+
///
89+
/// Panics if the `world.id()` does not equal the current [`QueryState`] internal id.
8190
pub fn validate_world_and_update_archetypes(&mut self, world: &World) {
8291
if world.id() != self.world_id {
8392
panic!("Attempted to use {} with a mismatched World. QueryStates can only be used with the World they were created from.",
@@ -93,6 +102,7 @@ where
93102
}
94103
}
95104

105+
/// Creates a new [`Archetype`].
96106
pub fn new_archetype(&mut self, archetype: &Archetype) {
97107
if self.fetch_state.matches_archetype(archetype)
98108
&& self.filter_state.matches_archetype(archetype)
@@ -116,6 +126,9 @@ where
116126
}
117127
}
118128

129+
/// Gets the query result for the given [`World`] and [`Entity`].
130+
///
131+
/// This can only be called for read-only queries, see [`Self::get_mut`] for write-queries.
119132
#[inline]
120133
pub fn get<'w, 's>(
121134
&'s mut self,
@@ -129,6 +142,7 @@ where
129142
unsafe { self.get_unchecked(world, entity) }
130143
}
131144

145+
/// Gets the query result for the given [`World`] and [`Entity`].
132146
#[inline]
133147
pub fn get_mut<'w, 's>(
134148
&'s mut self,
@@ -139,6 +153,8 @@ where
139153
unsafe { self.get_unchecked(world, entity) }
140154
}
141155

156+
/// Gets the query result for the given [`World`] and [`Entity`].
157+
///
142158
/// # Safety
143159
///
144160
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -158,7 +174,11 @@ where
158174
)
159175
}
160176

177+
/// Gets the query result for the given [`World`] and [`Entity`], where the last change and
178+
/// the current change tick are given.
179+
///
161180
/// # Safety
181+
///
162182
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
163183
/// have unique access to the components they query.
164184
pub unsafe fn get_unchecked_manual<'w, 's>(
@@ -193,6 +213,9 @@ where
193213
}
194214
}
195215

216+
/// Returns an [`Iterator`] over the query results for the given [`World`].
217+
///
218+
/// This can only be called for read-only queries, see [`Self::iter_mut`] for write-queries.
196219
#[inline]
197220
pub fn iter<'w, 's>(&'s mut self, world: &'w World) -> QueryIter<'w, 's, Q, F>
198221
where
@@ -202,12 +225,23 @@ where
202225
unsafe { self.iter_unchecked(world) }
203226
}
204227

228+
/// Returns an [`Iterator`] over the query results for the given [`World`].
205229
#[inline]
206230
pub fn iter_mut<'w, 's>(&'s mut self, world: &'w mut World) -> QueryIter<'w, 's, Q, F> {
207231
// SAFETY: query has unique world access
208232
unsafe { self.iter_unchecked(world) }
209233
}
210234

235+
/// Returns an [`Iterator`] over all possible combinations of `K` query results without repetition.
236+
/// This can only be called for read-only queries.
237+
///
238+
/// For permutations of size K of query returning N results, you will get:
239+
/// - if K == N: one permutation of all query results
240+
/// - if K < N: all possible K-sized combinations of query results, without repetition
241+
/// - if K > N: empty set (no K-sized combinations exist)
242+
///
243+
/// This can only be called for read-only queries, see [`Self::iter_combinations_mut`] for
244+
/// write-queries.
211245
#[inline]
212246
pub fn iter_combinations<'w, 's, const K: usize>(
213247
&'s mut self,
@@ -220,6 +254,13 @@ where
220254
unsafe { self.iter_combinations_unchecked(world) }
221255
}
222256

257+
/// Iterates over all possible combinations of `K` query results for the given [`World`]
258+
/// without repetition.
259+
///
260+
/// For permutations of size K of query returning N results, you will get:
261+
/// - if K == N: one permutation of all query results
262+
/// - if K < N: all possible K-sized combinations of query results, without repetition
263+
/// - if K > N: empty set (no K-sized combinations exist)
223264
#[inline]
224265
pub fn iter_combinations_mut<'w, 's, const K: usize>(
225266
&'s mut self,
@@ -229,6 +270,8 @@ where
229270
unsafe { self.iter_combinations_unchecked(world) }
230271
}
231272

273+
/// Returns an [`Iterator`] over the query results for the given [`World`].
274+
///
232275
/// # Safety
233276
///
234277
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -242,6 +285,10 @@ where
242285
self.iter_unchecked_manual(world, world.last_change_tick(), world.read_change_tick())
243286
}
244287

288+
/// Returns an [`Iterator`] over all possible combinations of `K` query results for the
289+
/// given [`World`] without repetition.
290+
/// This can only be called for read-only queries.
291+
///
245292
/// # Safety
246293
///
247294
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -259,7 +306,11 @@ where
259306
)
260307
}
261308

309+
/// Returns an [`Iterator`] for the given [`World`], where the last change and
310+
/// the current change tick are given.
311+
///
262312
/// # Safety
313+
///
263314
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
264315
/// have unique access to the components they query.
265316
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
@@ -274,7 +325,12 @@ where
274325
QueryIter::new(world, self, last_change_tick, change_tick)
275326
}
276327

328+
/// Returns an [`Iterator`] over all possible combinations of `K` query results for the
329+
/// given [`World`] without repetition.
330+
/// This can only be called for read-only queries.
331+
///
277332
/// # Safety
333+
///
278334
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
279335
/// have unique access to the components they query.
280336
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
@@ -289,6 +345,10 @@ where
289345
QueryCombinationIter::new(world, self, last_change_tick, change_tick)
290346
}
291347

348+
/// Runs `func` on each query result for the given [`World`]. This is faster than the equivalent
349+
/// iter() method, but cannot be chained like a normal [`Iterator`].
350+
///
351+
/// This can only be called for read-only queries, see [`Self::for_each_mut`] for write-queries.
292352
#[inline]
293353
pub fn for_each<'w, 's>(
294354
&'s mut self,
@@ -303,6 +363,8 @@ where
303363
}
304364
}
305365

366+
/// Runs `func` on each query result for the given [`World`]. This is faster than the equivalent
367+
/// iter_mut() method, but cannot be chained like a normal [`Iterator`].
306368
#[inline]
307369
pub fn for_each_mut<'w, 's>(
308370
&'s mut self,
@@ -315,6 +377,11 @@ where
315377
}
316378
}
317379

380+
/// Runs `func` on each query result for the given [`World`]. This is faster than the equivalent
381+
/// iter() method, but cannot be chained like a normal [`Iterator`].
382+
///
383+
/// This can only be called for read-only queries.
384+
///
318385
/// # Safety
319386
///
320387
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -334,6 +401,10 @@ where
334401
);
335402
}
336403

404+
/// Runs `func` on each query result in parallel using the given `task_pool`.
405+
///
406+
/// This can only be called for read-only queries, see [`Self::par_for_each_mut`] for
407+
/// write-queries.
337408
#[inline]
338409
pub fn par_for_each<'w, 's>(
339410
&'s mut self,
@@ -350,6 +421,7 @@ where
350421
}
351422
}
352423

424+
/// Runs `func` on each query result in parallel using the given `task_pool`.
353425
#[inline]
354426
pub fn par_for_each_mut<'w, 's>(
355427
&'s mut self,
@@ -364,6 +436,10 @@ where
364436
}
365437
}
366438

439+
/// Runs `func` on each query result in parallel using the given `task_pool`.
440+
///
441+
/// This can only be called for read-only queries.
442+
///
367443
/// # Safety
368444
///
369445
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -387,6 +463,10 @@ where
387463
);
388464
}
389465

466+
/// Runs `func` on each query result for the given [`World`], where the last change and
467+
/// the current change tick are given. This is faster than the equivalent
468+
/// iter() method, but cannot be chained like a normal [`Iterator`].
469+
///
390470
/// # Safety
391471
///
392472
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -439,6 +519,10 @@ where
439519
}
440520
}
441521

522+
/// Runs `func` on each query result in parallel for the given [`World`], where the last change and
523+
/// the current change tick are given. This is faster than the equivalent
524+
/// iter() method, but cannot be chained like a normal [`Iterator`].
525+
///
442526
/// # Safety
443527
///
444528
/// This does not check for mutable query correctness. To be safe, make sure mutable queries

0 commit comments

Comments
 (0)