Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Allow SystemParams with private fields #7056

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}
}

let (_impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let lifetimeless_generics: Vec<_> = generics
.params
Expand Down Expand Up @@ -441,12 +441,6 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}
}

let mut punctuated_type_generic_idents = Punctuated::<_, Token![,]>::new();
punctuated_type_generic_idents.extend(lifetimeless_generics.iter().filter_map(|g| match g {
GenericParam::Type(g) => Some(&g.ident),
_ => None,
}));

let mut punctuated_generic_idents = Punctuated::<_, Token![,]>::new();
punctuated_generic_idents.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => &g.ident,
Expand Down Expand Up @@ -485,24 +479,21 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
// The struct can still be accessed via SystemParam::State, e.g. EventReaderState can be accessed via
// <EventReader<'static, 'static, T> as SystemParam>::State
const _: () = {
impl<'w, 's, #punctuated_generics> #path::system::SystemParam for #struct_name #ty_generics #where_clause {
type State = State<'w, 's, #punctuated_generic_idents>;
impl #impl_generics #path::system::SystemParam for #struct_name #ty_generics #where_clause {
type State = FetchState<'static, 'static, #punctuated_generic_idents>;
}

#[doc(hidden)]
type State<'w, 's, #punctuated_generics_no_bounds> = FetchState<
(#(<#tuple_types as #path::system::SystemParam>::State,)*),
#punctuated_generic_idents
>;

#[doc(hidden)]
#state_struct_visibility struct FetchState <TSystemParamState, #punctuated_generics> {
state: TSystemParamState,
marker: std::marker::PhantomData<fn()->(#punctuated_type_generic_idents)>
#state_struct_visibility struct FetchState <'w, 's, #(#lifetimeless_generics,)*> {
state: (#(<#tuple_types as #path::system::SystemParam>::State,)*),
marker: std::marker::PhantomData<(
<#path::prelude::Query<'w, 's, ()> as #path::system::SystemParam>::State,
#(fn() -> #ignored_field_types,)*
)>,
}

unsafe impl<'__w, '__s, #punctuated_generics> #path::system::SystemParamState for
State<'__w, '__s, #punctuated_generic_idents>
unsafe impl<#punctuated_generics> #path::system::SystemParamState for
FetchState<'static, 'static, #punctuated_generic_idents>
#where_clause {
type Item<'w, 's> = #struct_name #ty_generics;

Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,4 +1809,10 @@ mod tests {
Res<'w, R>,
Local<'s, L>,
);

#[derive(Resource)]
struct PrivateResource;

#[derive(SystemParam)]
pub struct EncapsulatedParam<'w>(Res<'w, PrivateResource>);
}