Skip to content

Commit

Permalink
Support tuple structs with #[derive(SystemParam)] (bevyengine#6957)
Browse files Browse the repository at this point in the history
Currently, only named structs can be used with the `SystemParam` derive macro.

Remove the restriction. Tuple structs and unit structs are now supported.

---

+ Added support for tuple structs and unit structs to the `SystemParam` derive macro.
  • Loading branch information
JoJoJet committed Dec 21, 2022
1 parent 88390db commit ea9bee0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 14 additions & 6 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,14 @@ static SYSTEM_PARAM_ATTRIBUTE_NAME: &str = "system_param";
#[proc_macro_derive(SystemParam, attributes(system_param))]
pub fn derive_system_param(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let fields = match get_named_struct_fields(&ast.data) {
Ok(fields) => &fields.named,
Err(e) => return e.into_compile_error().into(),
let syn::Data::Struct(syn::DataStruct { fields: field_definitions, ..}) = ast.data else {
return syn::Error::new(ast.span(), "Invalid `SystemParam` type: expected a `struct`")
.into_compile_error()
.into();
};
let path = bevy_ecs_path();

let field_attributes = fields
let field_attributes = field_definitions
.iter()
.map(|field| {
(
Expand Down Expand Up @@ -352,9 +353,16 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
ignored_fields.push(field.ident.as_ref().unwrap());
ignored_field_types.push(&field.ty);
} else {
fields.push(field.ident.as_ref().unwrap());
let i = Index::from(i);
fields.push(
field
.ident
.as_ref()
.map(|f| quote! { #f })
.unwrap_or_else(|| quote! { #i }),
);
field_types.push(&field.ty);
field_indices.push(Index::from(i));
field_indices.push(i);
}
}

Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,13 @@ mod tests {
}

#[derive(SystemParam)]
pub struct UnitParam {}
pub struct UnitParam;

#[derive(SystemParam)]
pub struct TupleParam<'w, 's, R: Resource, L: FromWorld + Send + 'static>(
Res<'w, R>,
Local<'s, L>,
);

#[derive(SystemParam)]
struct MyParam<'w, T: Resource, Marker: 'static> {
Expand Down

0 comments on commit ea9bee0

Please sign in to comment.