@@ -13,6 +13,7 @@ use bevy_tasks::TaskPool;
13
13
use fixedbitset:: FixedBitSet ;
14
14
use thiserror:: Error ;
15
15
16
+ /// Provides scoped access to a [`World`] state according to a given [`WorldQuery`] and query filter.
16
17
pub struct QueryState < Q : WorldQuery , F : WorldQuery = ( ) >
17
18
where
18
19
F :: Fetch : FilterFetch ,
@@ -35,6 +36,7 @@ impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F>
35
36
where
36
37
F :: Fetch : FilterFetch ,
37
38
{
39
+ /// Creates a new [`QueryState`] from a given [`World`] and inherits the result of `world.id()`.
38
40
pub fn new ( world : & mut World ) -> Self {
39
41
let fetch_state = <Q :: State as FetchState >:: init ( world) ;
40
42
let filter_state = <F :: State as FetchState >:: init ( world) ;
68
70
state
69
71
}
70
72
73
+ /// Checks if the query is empty for the given [`World`], where the last change and current tick are given.
71
74
#[ inline]
72
75
pub fn is_empty ( & self , world : & World , last_change_tick : u32 , change_tick : u32 ) -> bool {
73
76
// SAFE: the iterator is instantly consumed via `none_remaining` and the implementation of
78
81
}
79
82
}
80
83
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.
81
90
pub fn validate_world_and_update_archetypes ( & mut self , world : & World ) {
82
91
if world. id ( ) != self . world_id {
83
92
panic ! ( "Attempted to use {} with a mismatched World. QueryStates can only be used with the World they were created from." ,
93
102
}
94
103
}
95
104
105
+ /// Creates a new [`Archetype`].
96
106
pub fn new_archetype ( & mut self , archetype : & Archetype ) {
97
107
if self . fetch_state . matches_archetype ( archetype)
98
108
&& self . filter_state . matches_archetype ( archetype)
@@ -116,6 +126,9 @@ where
116
126
}
117
127
}
118
128
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.
119
132
#[ inline]
120
133
pub fn get < ' w , ' s > (
121
134
& ' s mut self ,
@@ -129,6 +142,7 @@ where
129
142
unsafe { self . get_unchecked ( world, entity) }
130
143
}
131
144
145
+ /// Gets the query result for the given [`World`] and [`Entity`].
132
146
#[ inline]
133
147
pub fn get_mut < ' w , ' s > (
134
148
& ' s mut self ,
@@ -139,6 +153,8 @@ where
139
153
unsafe { self . get_unchecked ( world, entity) }
140
154
}
141
155
156
+ /// Gets the query result for the given [`World`] and [`Entity`].
157
+ ///
142
158
/// # Safety
143
159
///
144
160
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -158,7 +174,11 @@ where
158
174
)
159
175
}
160
176
177
+ /// Gets the query result for the given [`World`] and [`Entity`], where the last change and
178
+ /// the current change tick are given.
179
+ ///
161
180
/// # Safety
181
+ ///
162
182
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
163
183
/// have unique access to the components they query.
164
184
pub unsafe fn get_unchecked_manual < ' w , ' s > (
@@ -193,6 +213,9 @@ where
193
213
}
194
214
}
195
215
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.
196
219
#[ inline]
197
220
pub fn iter < ' w , ' s > ( & ' s mut self , world : & ' w World ) -> QueryIter < ' w , ' s , Q , F >
198
221
where
@@ -202,12 +225,23 @@ where
202
225
unsafe { self . iter_unchecked ( world) }
203
226
}
204
227
228
+ /// Returns an [`Iterator`] over the query results for the given [`World`].
205
229
#[ inline]
206
230
pub fn iter_mut < ' w , ' s > ( & ' s mut self , world : & ' w mut World ) -> QueryIter < ' w , ' s , Q , F > {
207
231
// SAFETY: query has unique world access
208
232
unsafe { self . iter_unchecked ( world) }
209
233
}
210
234
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.
211
245
#[ inline]
212
246
pub fn iter_combinations < ' w , ' s , const K : usize > (
213
247
& ' s mut self ,
@@ -220,6 +254,13 @@ where
220
254
unsafe { self . iter_combinations_unchecked ( world) }
221
255
}
222
256
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)
223
264
#[ inline]
224
265
pub fn iter_combinations_mut < ' w , ' s , const K : usize > (
225
266
& ' s mut self ,
@@ -229,6 +270,8 @@ where
229
270
unsafe { self . iter_combinations_unchecked ( world) }
230
271
}
231
272
273
+ /// Returns an [`Iterator`] over the query results for the given [`World`].
274
+ ///
232
275
/// # Safety
233
276
///
234
277
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -242,6 +285,10 @@ where
242
285
self . iter_unchecked_manual ( world, world. last_change_tick ( ) , world. read_change_tick ( ) )
243
286
}
244
287
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
+ ///
245
292
/// # Safety
246
293
///
247
294
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -259,7 +306,11 @@ where
259
306
)
260
307
}
261
308
309
+ /// Returns an [`Iterator`] for the given [`World`], where the last change and
310
+ /// the current change tick are given.
311
+ ///
262
312
/// # Safety
313
+ ///
263
314
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
264
315
/// have unique access to the components they query.
265
316
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
@@ -274,7 +325,12 @@ where
274
325
QueryIter :: new ( world, self , last_change_tick, change_tick)
275
326
}
276
327
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
+ ///
277
332
/// # Safety
333
+ ///
278
334
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
279
335
/// have unique access to the components they query.
280
336
/// This does not validate that `world.id()` matches `self.world_id`. Calling this on a `world`
@@ -289,6 +345,10 @@ where
289
345
QueryCombinationIter :: new ( world, self , last_change_tick, change_tick)
290
346
}
291
347
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.
292
352
#[ inline]
293
353
pub fn for_each < ' w , ' s > (
294
354
& ' s mut self ,
@@ -303,6 +363,8 @@ where
303
363
}
304
364
}
305
365
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`].
306
368
#[ inline]
307
369
pub fn for_each_mut < ' w , ' s > (
308
370
& ' s mut self ,
@@ -315,6 +377,11 @@ where
315
377
}
316
378
}
317
379
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
+ ///
318
385
/// # Safety
319
386
///
320
387
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -334,6 +401,10 @@ where
334
401
) ;
335
402
}
336
403
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.
337
408
#[ inline]
338
409
pub fn par_for_each < ' w , ' s > (
339
410
& ' s mut self ,
@@ -350,6 +421,7 @@ where
350
421
}
351
422
}
352
423
424
+ /// Runs `func` on each query result in parallel using the given `task_pool`.
353
425
#[ inline]
354
426
pub fn par_for_each_mut < ' w , ' s > (
355
427
& ' s mut self ,
@@ -364,6 +436,10 @@ where
364
436
}
365
437
}
366
438
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
+ ///
367
443
/// # Safety
368
444
///
369
445
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -387,6 +463,10 @@ where
387
463
) ;
388
464
}
389
465
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
+ ///
390
470
/// # Safety
391
471
///
392
472
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
@@ -439,6 +519,10 @@ where
439
519
}
440
520
}
441
521
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
+ ///
442
526
/// # Safety
443
527
///
444
528
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
0 commit comments