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.
This commit is contained in:
parent
cf480d939a
commit
0363e0b32a
@ -327,13 +327,14 @@ static SYSTEM_PARAM_ATTRIBUTE_NAME: &str = "system_param";
|
|||||||
#[proc_macro_derive(SystemParam, attributes(system_param))]
|
#[proc_macro_derive(SystemParam, attributes(system_param))]
|
||||||
pub fn derive_system_param(input: TokenStream) -> TokenStream {
|
pub fn derive_system_param(input: TokenStream) -> TokenStream {
|
||||||
let ast = parse_macro_input!(input as DeriveInput);
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
let fields = match get_named_struct_fields(&ast.data) {
|
let syn::Data::Struct(syn::DataStruct { fields: field_definitions, ..}) = ast.data else {
|
||||||
Ok(fields) => &fields.named,
|
return syn::Error::new(ast.span(), "Invalid `SystemParam` type: expected a `struct`")
|
||||||
Err(e) => return e.into_compile_error().into(),
|
.into_compile_error()
|
||||||
|
.into();
|
||||||
};
|
};
|
||||||
let path = bevy_ecs_path();
|
let path = bevy_ecs_path();
|
||||||
|
|
||||||
let field_attributes = fields
|
let field_attributes = field_definitions
|
||||||
.iter()
|
.iter()
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
(
|
(
|
||||||
@ -368,9 +369,16 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
|
|||||||
ignored_fields.push(field.ident.as_ref().unwrap());
|
ignored_fields.push(field.ident.as_ref().unwrap());
|
||||||
ignored_field_types.push(&field.ty);
|
ignored_field_types.push(&field.ty);
|
||||||
} else {
|
} 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_types.push(&field.ty);
|
||||||
field_indices.push(Index::from(i));
|
field_indices.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1662,5 +1662,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SystemParam)]
|
#[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>,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user