@@ -8,7 +8,7 @@ use crate::{
8
8
world:: { Mut , World } ,
9
9
} ;
10
10
use bevy_ecs_macros:: all_tuples;
11
- pub use bevy_ecs_macros:: WorldQuery ;
11
+ pub use bevy_ecs_macros:: { WorldQuery , WorldQueryFilter } ;
12
12
use bevy_ptr:: { ThinSlicePtr , UnsafeCellDeref } ;
13
13
use std:: { cell:: UnsafeCell , marker:: PhantomData } ;
14
14
@@ -28,19 +28,11 @@ use std::{cell::UnsafeCell, marker::PhantomData};
28
28
/// - `Option<WQ>`: Queries the inner [`WorldQuery`] `WQ` but instead of discarding the entity if the world
29
29
/// query fails it returns [`None`]. See [`Query`](crate::system::Query).
30
30
/// - `(WQ1, WQ2, ...)`: Queries all contained world queries allowing to query for more than one thing.
31
- /// This is the `And` operator for filters. See [`Or`].
31
+ /// This is the `And` operator for filters. See [`Or`](crate::query::Or) .
32
32
/// - `ChangeTrackers<C>`: See the docs of [`ChangeTrackers`].
33
33
/// - [`Entity`]: Using the entity type as a world query will grant access to the entity that is
34
34
/// being queried for. See [`Entity`].
35
35
///
36
- /// Bevy also offers a few filters like [`Added`](crate::query::Added), [`Changed`](crate::query::Changed),
37
- /// [`With`](crate::query::With), [`Without`](crate::query::Without) and [`Or`].
38
- /// For more information on these consult the item's corresponding documentation.
39
- ///
40
- /// [`Or`]: crate::query::Or
41
- ///
42
- /// # Derive
43
- ///
44
36
/// This trait can be derived with the [`derive@super::WorldQuery`] macro.
45
37
///
46
38
/// You may want to implement a custom query with the derive macro for the following reasons:
@@ -260,14 +252,28 @@ use std::{cell::UnsafeCell, marker::PhantomData};
260
252
///
261
253
/// # bevy_ecs::system::assert_is_system(my_system);
262
254
/// ```
255
+ pub trait WorldQuery : for < ' w > WorldQueryGats < ' w , _State = Self :: State > {
256
+ type State : FetchState ;
257
+
258
+ /// This function manually implements variance for the query items.
259
+ fn shrink < ' wlong : ' wshort , ' wshort > ( item : QueryItem < ' wlong , Self > ) -> QueryItem < ' wshort , Self > ;
260
+ }
261
+
262
+ /// Types that can be used as a query filter for queries from a [`World`].
263
+ ///
264
+ /// Bevy offers a few filters like [`Added`](crate::query::Added), [`Changed`](crate::query::Changed),
265
+ /// [`With`](crate::query::With), [`Without`](crate::query::Without) and [`Or`].
266
+ /// For more information on these consult the item's corresponding documentation.
263
267
///
264
- /// ## Filters
268
+ /// [`Or`]: crate::query::Or
265
269
///
266
- /// Using [`derive@super::WorldQuery`] macro we can create our own query filters.
270
+ /// # Derive
271
+ ///
272
+ /// Using [`derive@super::WorldQueryFilter`] macro we can create our own query filters.
267
273
///
268
274
/// ```
269
275
/// # use bevy_ecs::prelude::*;
270
- /// use bevy_ecs::{query::WorldQuery , component::Component};
276
+ /// use bevy_ecs::{query::WorldQueryFilter , component::Component};
271
277
///
272
278
/// #[derive(Component)]
273
279
/// struct Foo;
@@ -278,7 +284,7 @@ use std::{cell::UnsafeCell, marker::PhantomData};
278
284
/// #[derive(Component)]
279
285
/// struct Qux;
280
286
///
281
- /// #[derive(WorldQuery )]
287
+ /// #[derive(WorldQueryFilter )]
282
288
/// struct MyFilter<T: Component, P: Component> {
283
289
/// _foo: With<Foo>,
284
290
/// _bar: With<Bar>,
@@ -292,12 +298,22 @@ use std::{cell::UnsafeCell, marker::PhantomData};
292
298
///
293
299
/// # bevy_ecs::system::assert_is_system(my_system);
294
300
/// ```
295
- pub trait WorldQuery : for < ' w > WorldQueryGats < ' w , _State = Self :: State > {
296
- type State : FetchState ;
297
-
298
- /// This function manually implements variance for the query items.
299
- fn shrink < ' wlong : ' wshort , ' wshort > ( item : QueryItem < ' wlong , Self > ) -> QueryItem < ' wshort , Self > ;
300
- }
301
+ ///
302
+ /// **Note:** component types themselves can't be used as filters:
303
+ ///
304
+ /// ```compile_fail
305
+ /// # use bevy_ecs::prelude::*;
306
+ /// use bevy_ecs::query::WorldQueryFilter;
307
+ ///
308
+ /// #[derive(Component)]
309
+ /// struct Foo;
310
+ ///
311
+ /// #[derive(WorldQueryFilter)]
312
+ /// struct FooFilter {
313
+ /// foo: &'static Foo,
314
+ /// }
315
+ /// ```
316
+ pub trait WorldQueryFilter : WorldQuery { }
301
317
302
318
/// The [`Fetch`] of a [`WorldQuery`], which declares which data it needs access to
303
319
pub type QueryFetch < ' w , Q > = <Q as WorldQueryGats < ' w > >:: Fetch ;
@@ -1555,6 +1571,11 @@ macro_rules! impl_tuple_fetch {
1555
1571
}
1556
1572
}
1557
1573
1574
+ #[ allow( non_snake_case) ]
1575
+ #[ allow( clippy:: unused_unit) ]
1576
+ impl <$( $name: WorldQueryFilter ) ,* > WorldQueryFilter for ( $( $name, ) * ) {
1577
+ }
1578
+
1558
1579
/// SAFETY: each item in the tuple is read only
1559
1580
unsafe impl <' w, $( $name: ReadOnlyFetch ) ,* > ReadOnlyFetch for ( $( $name, ) * ) { }
1560
1581
0 commit comments