@@ -368,51 +368,55 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
368
368
} ) ) ;
369
369
370
370
let struct_name = & ast. ident ;
371
- let fetch_struct_name = Ident :: new ( & format ! ( "{}State" , struct_name) , Span :: call_site ( ) ) ;
372
371
let fetch_struct_visibility = & ast. vis ;
373
372
374
373
TokenStream :: from ( quote ! {
375
- impl #impl_generics #path:: system:: SystemParam for #struct_name #ty_generics #where_clause {
376
- type Fetch = #fetch_struct_name <( #( <#field_types as #path:: system:: SystemParam >:: Fetch , ) * ) , #punctuated_generic_idents>;
377
- }
374
+ // We define the FetchState struct in an anonymous scope to avoid polluting the user namespace.
375
+ // The struct can still be accessed via SystemParam::Fetch, e.g. EventReaderState can be accessed via
376
+ // <EventReader<'static, 'static, T> as SystemParam>::Fetch
377
+ const _: ( ) = {
378
+ impl #impl_generics #path:: system:: SystemParam for #struct_name #ty_generics #where_clause {
379
+ type Fetch = FetchState <( #( <#field_types as #path:: system:: SystemParam >:: Fetch , ) * ) , #punctuated_generic_idents>;
380
+ }
378
381
379
- #[ doc( hidden) ]
380
- #fetch_struct_visibility struct #fetch_struct_name <TSystemParamState , #punctuated_generic_idents> {
381
- state: TSystemParamState ,
382
- marker: std:: marker:: PhantomData <fn ( ) ->( #punctuated_generic_idents) >
383
- }
382
+ #[ doc( hidden) ]
383
+ #fetch_struct_visibility struct FetchState <TSystemParamState , #punctuated_generic_idents> {
384
+ state: TSystemParamState ,
385
+ marker: std:: marker:: PhantomData <fn ( ) ->( #punctuated_generic_idents) >
386
+ }
384
387
385
- unsafe impl <TSystemParamState : #path:: system:: SystemParamState , #punctuated_generics> #path:: system:: SystemParamState for #fetch_struct_name<TSystemParamState , #punctuated_generic_idents> #where_clause {
386
- fn init( world: & mut #path:: world:: World , system_meta: & mut #path:: system:: SystemMeta ) -> Self {
387
- Self {
388
- state: TSystemParamState :: init( world, system_meta) ,
389
- marker: std:: marker:: PhantomData ,
388
+ unsafe impl <TSystemParamState : #path:: system:: SystemParamState , #punctuated_generics> #path:: system:: SystemParamState for FetchState <TSystemParamState , #punctuated_generic_idents> #where_clause {
389
+ fn init( world: & mut #path:: world:: World , system_meta: & mut #path:: system:: SystemMeta ) -> Self {
390
+ Self {
391
+ state: TSystemParamState :: init( world, system_meta) ,
392
+ marker: std:: marker:: PhantomData ,
393
+ }
390
394
}
391
- }
392
395
393
- fn new_archetype( & mut self , archetype: & #path:: archetype:: Archetype , system_meta: & mut #path:: system:: SystemMeta ) {
394
- self . state. new_archetype( archetype, system_meta)
395
- }
396
+ fn new_archetype( & mut self , archetype: & #path:: archetype:: Archetype , system_meta: & mut #path:: system:: SystemMeta ) {
397
+ self . state. new_archetype( archetype, system_meta)
398
+ }
396
399
397
- fn apply( & mut self , world: & mut #path:: world:: World ) {
398
- self . state. apply( world)
400
+ fn apply( & mut self , world: & mut #path:: world:: World ) {
401
+ self . state. apply( world)
402
+ }
399
403
}
400
- }
401
404
402
- impl #impl_generics #path:: system:: SystemParamFetch <' w, ' s> for #fetch_struct_name <( #( <#field_types as #path:: system:: SystemParam >:: Fetch , ) * ) , #punctuated_generic_idents> #where_clause {
403
- type Item = #struct_name #ty_generics;
404
- unsafe fn get_param(
405
- state: & ' s mut Self ,
406
- system_meta: & #path:: system:: SystemMeta ,
407
- world: & ' w #path:: world:: World ,
408
- change_tick: u32 ,
409
- ) -> Self :: Item {
410
- #struct_name {
411
- #( #fields: <<#field_types as #path:: system:: SystemParam >:: Fetch as #path:: system:: SystemParamFetch >:: get_param( & mut state. state. #field_indices, system_meta, world, change_tick) , ) *
412
- #( #ignored_fields: <#ignored_field_types>:: default ( ) , ) *
405
+ impl #impl_generics #path:: system:: SystemParamFetch <' w, ' s> for FetchState <( #( <#field_types as #path:: system:: SystemParam >:: Fetch , ) * ) , #punctuated_generic_idents> #where_clause {
406
+ type Item = #struct_name #ty_generics;
407
+ unsafe fn get_param(
408
+ state: & ' s mut Self ,
409
+ system_meta: & #path:: system:: SystemMeta ,
410
+ world: & ' w #path:: world:: World ,
411
+ change_tick: u32 ,
412
+ ) -> Self :: Item {
413
+ #struct_name {
414
+ #( #fields: <<#field_types as #path:: system:: SystemParam >:: Fetch as #path:: system:: SystemParamFetch >:: get_param( & mut state. state. #field_indices, system_meta, world, change_tick) , ) *
415
+ #( #ignored_fields: <#ignored_field_types>:: default ( ) , ) *
416
+ }
413
417
}
414
418
}
415
- }
419
+ } ;
416
420
} )
417
421
}
418
422
0 commit comments