Skip to content

Commit

Permalink
add an attribute for specifying unconditionally readonly or mutable p…
Browse files Browse the repository at this point in the history
…arams
  • Loading branch information
joseph-gio committed Dec 28, 2022
1 parent 856702c commit 9daf39f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ struct SystemParamFieldAttributes {

static SYSTEM_PARAM_ATTRIBUTE_NAME: &str = "system_param";

#[derive(Debug)]
enum ParamAccess {
Auto,
ReadOnly,
Mutable,
}

/// Implement `SystemParam` to use a struct as a parameter in a system
#[proc_macro_derive(SystemParam, attributes(system_param))]
pub fn derive_system_param(input: TokenStream) -> TokenStream {
Expand All @@ -344,6 +351,28 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
};
let path = bevy_ecs_path();

let mut access = ParamAccess::Auto;
for attr in &ast.attrs {
let expected_attr_name = format_ident!("{SYSTEM_PARAM_ATTRIBUTE_NAME}");
if attr.path.get_ident() != Some(&expected_attr_name) {
continue;
}

syn::custom_keyword!(read_only);
syn::custom_keyword!(mutable);

attr.parse_args_with(|input: ParseStream| {
if input.parse::<Option<read_only>>()?.is_some() {
access = ParamAccess::ReadOnly;
}
if input.parse::<Option<mutable>>()?.is_some() {
access = ParamAccess::Mutable;
}
Ok(())
})
.expect("Invalid 'system_param' attribute format.");
}

let field_attributes = field_definitions
.iter()
.map(|field| {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ mod tests {
);

#[derive(SystemParam)]
#[system_param(read_only)]
pub struct EncapsulatedParam<'s> {
p: PrivateParam<'s>,
}
Expand Down

0 comments on commit 9daf39f

Please sign in to comment.