Skip to content

Commit 0363e0b

Browse files
committed
Support tuple structs with #[derive(SystemParam)] (#6957)
# Objective Currently, only named structs can be used with the `SystemParam` derive macro. ## Solution Remove the restriction. Tuple structs and unit structs are now supported. --- ## Changelog + Added support for tuple structs and unit structs to the `SystemParam` derive macro.
1 parent cf480d9 commit 0363e0b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,14 @@ static SYSTEM_PARAM_ATTRIBUTE_NAME: &str = "system_param";
327327
#[proc_macro_derive(SystemParam, attributes(system_param))]
328328
pub fn derive_system_param(input: TokenStream) -> TokenStream {
329329
let ast = parse_macro_input!(input as DeriveInput);
330-
let fields = match get_named_struct_fields(&ast.data) {
331-
Ok(fields) => &fields.named,
332-
Err(e) => return e.into_compile_error().into(),
330+
let syn::Data::Struct(syn::DataStruct { fields: field_definitions, ..}) = ast.data else {
331+
return syn::Error::new(ast.span(), "Invalid `SystemParam` type: expected a `struct`")
332+
.into_compile_error()
333+
.into();
333334
};
334335
let path = bevy_ecs_path();
335336

336-
let field_attributes = fields
337+
let field_attributes = field_definitions
337338
.iter()
338339
.map(|field| {
339340
(
@@ -368,9 +369,16 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
368369
ignored_fields.push(field.ident.as_ref().unwrap());
369370
ignored_field_types.push(&field.ty);
370371
} else {
371-
fields.push(field.ident.as_ref().unwrap());
372+
let i = Index::from(i);
373+
fields.push(
374+
field
375+
.ident
376+
.as_ref()
377+
.map(|f| quote! { #f })
378+
.unwrap_or_else(|| quote! { #i }),
379+
);
372380
field_types.push(&field.ty);
373-
field_indices.push(Index::from(i));
381+
field_indices.push(i);
374382
}
375383
}
376384

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,5 +1662,11 @@ mod tests {
16621662
}
16631663

16641664
#[derive(SystemParam)]
1665-
pub struct UnitParam {}
1665+
pub struct UnitParam;
1666+
1667+
#[derive(SystemParam)]
1668+
pub struct TupleParam<'w, 's, R: Resource, L: FromWorld + Send + 'static>(
1669+
Res<'w, R>,
1670+
Local<'s, L>,
1671+
);
16661672
}

0 commit comments

Comments
 (0)