@@ -328,6 +328,9 @@ pub unsafe trait WorldQuery {
328
328
/// constructing [`Self::Fetch`](crate::query::WorldQuery::Fetch).
329
329
type State : Send + Sync + Sized ;
330
330
331
+ /// Runtime config value passed to [`WorldQuery::init_state`]
332
+ type Config ;
333
+
331
334
/// This function manually implements subtyping for the query items.
332
335
fn shrink < ' wlong : ' wshort , ' wshort > ( item : Self :: Item < ' wlong > ) -> Self :: Item < ' wshort > ;
333
336
@@ -434,7 +437,9 @@ pub unsafe trait WorldQuery {
434
437
) ;
435
438
436
439
/// Creates and initializes a [`State`](WorldQuery::State) for this [`WorldQuery`] type.
437
- fn init_state ( world : & mut World ) -> Self :: State ;
440
+ ///
441
+ /// [`WorldQuery::Config`] is used to pass runtime data for dynamic queries.
442
+ fn init_state ( config : Self :: Config , world : & mut World ) -> Self :: State ;
438
443
439
444
/// Returns `true` if this query matches a set of components. Otherwise, returns `false`.
440
445
fn matches_component_set (
@@ -465,6 +470,7 @@ unsafe impl WorldQuery for Entity {
465
470
type Item < ' w > = Entity ;
466
471
type ReadOnly = Self ;
467
472
type State = ( ) ;
473
+ type Config = ( ) ;
468
474
469
475
fn shrink < ' wlong : ' wshort , ' wshort > ( item : Self :: Item < ' wlong > ) -> Self :: Item < ' wshort > {
470
476
item
@@ -515,7 +521,7 @@ unsafe impl WorldQuery for Entity {
515
521
) {
516
522
}
517
523
518
- fn init_state ( _world : & mut World ) { }
524
+ fn init_state ( _config : ( ) , _world : & mut World ) { }
519
525
520
526
fn matches_component_set (
521
527
_state : & Self :: State ,
@@ -542,6 +548,7 @@ unsafe impl<T: Component> WorldQuery for &T {
542
548
type Item < ' w > = & ' w T ;
543
549
type ReadOnly = Self ;
544
550
type State = ComponentId ;
551
+ type Config = ( ) ;
545
552
546
553
fn shrink < ' wlong : ' wshort , ' wshort > ( item : & ' wlong T ) -> & ' wshort T {
547
554
item
@@ -652,7 +659,7 @@ unsafe impl<T: Component> WorldQuery for &T {
652
659
}
653
660
}
654
661
655
- fn init_state ( world : & mut World ) -> ComponentId {
662
+ fn init_state ( _config : ( ) , world : & mut World ) -> ComponentId {
656
663
world. init_component :: < T > ( )
657
664
}
658
665
@@ -688,6 +695,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
688
695
type Item < ' w > = Ref < ' w , T > ;
689
696
type ReadOnly = Self ;
690
697
type State = ComponentId ;
698
+ type Config = ( ) ;
691
699
692
700
fn shrink < ' wlong : ' wshort , ' wshort > ( item : Ref < ' wlong , T > ) -> Ref < ' wshort , T > {
693
701
item
@@ -814,7 +822,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
814
822
}
815
823
}
816
824
817
- fn init_state ( world : & mut World ) -> ComponentId {
825
+ fn init_state ( _config : ( ) , world : & mut World ) -> ComponentId {
818
826
world. init_component :: < T > ( )
819
827
}
820
828
@@ -850,6 +858,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
850
858
type Item < ' w > = Mut < ' w , T > ;
851
859
type ReadOnly = & ' __w T ;
852
860
type State = ComponentId ;
861
+ type Config = ( ) ;
853
862
854
863
fn shrink < ' wlong : ' wshort , ' wshort > ( item : Mut < ' wlong , T > ) -> Mut < ' wshort , T > {
855
864
item
@@ -976,7 +985,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
976
985
}
977
986
}
978
987
979
- fn init_state ( world : & mut World ) -> ComponentId {
988
+ fn init_state ( _config : ( ) , world : & mut World ) -> ComponentId {
980
989
world. init_component :: < T > ( )
981
990
}
982
991
@@ -1000,6 +1009,7 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
1000
1009
type Item < ' w > = Option < T :: Item < ' w > > ;
1001
1010
type ReadOnly = Option < T :: ReadOnly > ;
1002
1011
type State = T :: State ;
1012
+ type Config = T :: Config ;
1003
1013
1004
1014
fn shrink < ' wlong : ' wshort , ' wshort > ( item : Self :: Item < ' wlong > ) -> Self :: Item < ' wshort > {
1005
1015
item. map ( T :: shrink)
@@ -1081,8 +1091,8 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
1081
1091
}
1082
1092
}
1083
1093
1084
- fn init_state ( world : & mut World ) -> T :: State {
1085
- T :: init_state ( world)
1094
+ fn init_state ( config : Self :: Config , world : & mut World ) -> T :: State {
1095
+ T :: init_state ( config , world)
1086
1096
}
1087
1097
1088
1098
fn matches_component_set (
@@ -1106,6 +1116,7 @@ macro_rules! impl_tuple_fetch {
1106
1116
type Item <' w> = ( $( $name:: Item <' w>, ) * ) ;
1107
1117
type ReadOnly = ( $( $name:: ReadOnly , ) * ) ;
1108
1118
type State = ( $( $name:: State , ) * ) ;
1119
+ type Config = ( $( $name:: Config , ) * ) ;
1109
1120
1110
1121
fn shrink<' wlong: ' wshort, ' wshort>( item: Self :: Item <' wlong>) -> Self :: Item <' wshort> {
1111
1122
let ( $( $name, ) * ) = item;
@@ -1183,8 +1194,9 @@ macro_rules! impl_tuple_fetch {
1183
1194
}
1184
1195
1185
1196
1186
- fn init_state( _world: & mut World ) -> Self :: State {
1187
- ( $( $name:: init_state( _world) , ) * )
1197
+ fn init_state( config: Self :: Config , _world: & mut World ) -> Self :: State {
1198
+ let ( $( $state, ) * ) = config;
1199
+ ( $( $name:: init_state( $state, _world) , ) * )
1188
1200
}
1189
1201
1190
1202
fn matches_component_set( state: & Self :: State , _set_contains_id: & impl Fn ( ComponentId ) -> bool ) -> bool {
@@ -1216,6 +1228,7 @@ macro_rules! impl_anytuple_fetch {
1216
1228
type Item <' w> = ( $( Option <$name:: Item <' w>>, ) * ) ;
1217
1229
type ReadOnly = AnyOf <( $( $name:: ReadOnly , ) * ) >;
1218
1230
type State = ( $( $name:: State , ) * ) ;
1231
+ type Config = ( $( $name:: Config , ) * ) ;
1219
1232
1220
1233
fn shrink<' wlong: ' wshort, ' wshort>( item: Self :: Item <' wlong>) -> Self :: Item <' wshort> {
1221
1234
let ( $( $name, ) * ) = item;
@@ -1313,8 +1326,9 @@ macro_rules! impl_anytuple_fetch {
1313
1326
) *
1314
1327
}
1315
1328
1316
- fn init_state( _world: & mut World ) -> Self :: State {
1317
- ( $( $name:: init_state( _world) , ) * )
1329
+ fn init_state( config: Self :: Config , _world: & mut World ) -> Self :: State {
1330
+ let ( $( $state, ) * ) = config;
1331
+ ( $( $name:: init_state( $state, _world) , ) * )
1318
1332
}
1319
1333
1320
1334
fn matches_component_set( _state: & Self :: State , _set_contains_id: & impl Fn ( ComponentId ) -> bool ) -> bool {
@@ -1342,6 +1356,7 @@ unsafe impl<Q: WorldQuery> WorldQuery for NopWorldQuery<Q> {
1342
1356
type Item < ' w > = ( ) ;
1343
1357
type ReadOnly = Self ;
1344
1358
type State = Q :: State ;
1359
+ type Config = Q :: Config ;
1345
1360
1346
1361
fn shrink < ' wlong : ' wshort , ' wshort > ( _: ( ) ) { }
1347
1362
@@ -1383,8 +1398,8 @@ unsafe impl<Q: WorldQuery> WorldQuery for NopWorldQuery<Q> {
1383
1398
) {
1384
1399
}
1385
1400
1386
- fn init_state ( world : & mut World ) -> Self :: State {
1387
- Q :: init_state ( world)
1401
+ fn init_state ( config : Self :: Config , world : & mut World ) -> Self :: State {
1402
+ Q :: init_state ( config , world)
1388
1403
}
1389
1404
1390
1405
fn matches_component_set (
@@ -1404,6 +1419,7 @@ unsafe impl<T: ?Sized> WorldQuery for PhantomData<T> {
1404
1419
type Fetch < ' a > = ( ) ;
1405
1420
type ReadOnly = Self ;
1406
1421
type State = ( ) ;
1422
+ type Config = ( ) ;
1407
1423
1408
1424
fn shrink < ' wlong : ' wshort , ' wshort > ( _item : Self :: Item < ' wlong > ) -> Self :: Item < ' wshort > { }
1409
1425
@@ -1450,7 +1466,7 @@ unsafe impl<T: ?Sized> WorldQuery for PhantomData<T> {
1450
1466
) {
1451
1467
}
1452
1468
1453
- fn init_state ( _world : & mut World ) -> Self :: State { }
1469
+ fn init_state ( _config : Self :: Config , _world : & mut World ) -> Self :: State { }
1454
1470
1455
1471
fn matches_component_set (
1456
1472
_state : & Self :: State ,
0 commit comments