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

derive(SystemParam) better lifetime param (#10331) #13794

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Wuketuke
Copy link
Contributor

@Wuketuke Wuketuke commented Jun 10, 2024

previously, only 'w and 's lifetime parameters were permitted. now, 'world and 'state are permitted aswell.
this is achieved by replacing every occurrence of 'world with 'w, and 'state with 's.
I also updated the documentation to inform the user that they may use these new lifetimes now.
That is, in the compiler error message, and in the description of the trait (idk if there are more places that i should have updated).

it seems like a hack, and I am not confident if this is the perfect and best way to solve the issue. Especially since I modified the Cargo.toml in bevy_macro_utils
And it took me way longer than id like to admit bc I thought I was smarter than ChatGpt lol

Testing

I wrote some boilerplate code in the trait documentation in such a way that its hidden to the user. It seems like a hack aswell, but idk how else to unit test if code compiles

fixes #10331

previously, only 'w and 's lifetime parameters were permitted.
now, 'world and 'state are permitted aswell.
this is achieved by replacing every occurance of 'world with 'w, and likewise with state
@Wuketuke
Copy link
Contributor Author

i feel so silly every time the tests fail

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Macros Code that generates Rust code labels Jun 10, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Not bad, if a bit hacky. Good to see doc tests!

Would it be possible to allow for any lifetime names here instead?

@Wuketuke
Copy link
Contributor Author

i made it so any lifetime names are possible. but i hate it, it looks gross haha
there is probably a better way to do this, but im new to rust (and syn), so i dont know

/// 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 {
let input = replace_lifetimes(
input,
Box::new(|s| match s {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should just not special-case lifetime names at all 🤔 Are we able to do so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I respect your intuition, but i dont understand it. the whole point of that function is to abbreviate lifetimes, since 'world and 'state should be a special exception; replacing other things like variable names or punctuation is really not what were after.
Honestly id go back to the previous iteration where the values were hardcoded, and declare the function inside the only place where it is used (if rust allows that)
maybe someone with macro experience can actually find a more elegant solution, but idk if there is one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that and solve the issue raised directly.

@alice-i-cecile
Copy link
Member

I wish I was better at macros so I could give you better help myself!

@Wuketuke
Copy link
Contributor Author

@alice-i-cecile also i modified one Cargo.toml file (in bevy_macro_utils), and idk i am allowed to just do that

PS: should i explicitly mention people in these discussions, or are they notified automatically?

@alice-i-cecile
Copy link
Member

Yeah, if you need more features from syn just adding features here is totally fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use D-Macros Code that generates Rust code S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

derive(SystemParam) should allow 'world and 'state lifetime names
2 participants