Skip to content

Commit 04c671a

Browse files
committed
move system_param fetch struct into anonymous scope to avoid name collisions (#4100)
# Objective avoid naming collisions with user structs when deriving ``system_param``. ## Solution ~rename the fetch struct created by ``#[derive(system_param)]`` from ``{}State`` to ``{}SysParamState``.~ place the fetch struct into an anonymous scope. ## Migration Guide For code that was using a system param's fetch struct, such as ``EventReader``'s ``EventReaderState``, the fetch struct can now be identified via the SystemParam trait associated type ``Fetch``, e.g. for ``EventReader<T>`` it can be identified as ``<EventReader<'static, 'static, T> as SystemParam>::Fetch``
1 parent ae6a6d4 commit 04c671a

File tree

1 file changed

+37
-33
lines changed
  • crates/bevy_ecs/macros/src

1 file changed

+37
-33
lines changed

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -368,51 +368,55 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
368368
}));
369369

370370
let struct_name = &ast.ident;
371-
let fetch_struct_name = Ident::new(&format!("{}State", struct_name), Span::call_site());
372371
let fetch_struct_visibility = &ast.vis;
373372

374373
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+
}
378381

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+
}
384387

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+
}
390394
}
391-
}
392395

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+
}
396399

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+
}
399403
}
400-
}
401404

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+
}
413417
}
414418
}
415-
}
419+
};
416420
})
417421
}
418422

0 commit comments

Comments
 (0)