Skip to content

Commit 5597fc5

Browse files
committed
Add documentation to QueryCombinationIter (#5739)
# Objective - Document `QueryCombinationIter` ## Solution - Describe the item, add usage and examples - Copy notes about the number of query items generated from the corresponding query methods (they will be removed in #5742 ([motivation])) ## Additional notes - Derived from #4989 [motivation]: #4989 (comment)
1 parent 584d855 commit 5597fc5

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

crates/bevy_ecs/src/query/iter.rs

+53
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,59 @@ where
214214
{
215215
}
216216

217+
/// An iterator over `K`-sized combinations of query items without repetition.
218+
///
219+
/// In this context, a combination is an unordered subset of `K` elements.
220+
/// The number of combinations depend on how `K` relates to the number of entities matching the [`Query`] (called `N`):
221+
/// - if `K = N`, only one combination exists,
222+
/// - if `K < N`, there are <sub>N</sub>C<sub>K</sub> combinations (see the [performance section] of `Query`),
223+
/// - if `K > N`, there are no combinations.
224+
///
225+
/// # Usage
226+
///
227+
/// This type is returned by calling [`Query::iter_combinations`] or [`Query::iter_combinations_mut`].
228+
///
229+
/// It implements [`Iterator`] only if it iterates over read-only query items ([learn more]).
230+
///
231+
/// In the case of mutable query items, it can be iterated by calling [`fetch_next`] in a `while let` loop.
232+
///
233+
/// # Examples
234+
///
235+
/// The following example shows how to traverse the iterator when the query items are read-only.
236+
///
237+
/// ```
238+
/// # use bevy_ecs::prelude::*;
239+
/// # #[derive(Component)]
240+
/// # struct ComponentA;
241+
/// #
242+
/// fn some_system(query: Query<&ComponentA>) {
243+
/// for [a1, a2] in query.iter_combinations() {
244+
/// // ...
245+
/// }
246+
/// }
247+
/// ```
248+
///
249+
/// The following example shows how `fetch_next` should be called with a `while let` loop to traverse the iterator when the query items are mutable.
250+
///
251+
/// ```
252+
/// # use bevy_ecs::prelude::*;
253+
/// # #[derive(Component)]
254+
/// # struct ComponentA;
255+
/// #
256+
/// fn some_system(mut query: Query<&mut ComponentA>) {
257+
/// let mut combinations = query.iter_combinations_mut();
258+
/// while let Some([a1, a2]) = combinations.fetch_next() {
259+
/// // ...
260+
/// }
261+
/// }
262+
/// ```
263+
///
264+
/// [`fetch_next`]: Self::fetch_next
265+
/// [learn more]: Self#impl-Iterator
266+
/// [performance section]: crate::system::Query#performance
267+
/// [`Query`]: crate::system::Query
268+
/// [`Query::iter_combinations`]: crate::system::Query::iter_combinations
269+
/// [`Query::iter_combinations_mut`]: crate::system::Query::iter_combinations_mut
217270
pub struct QueryCombinationIter<'w, 's, Q: WorldQuery, F: WorldQuery, const K: usize> {
218271
tables: &'w Tables,
219272
archetypes: &'w Archetypes,

0 commit comments

Comments
 (0)