-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
allow Call
and Closure
expressions in hook macro attributes
#18017
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just reviewed macro code!
@Bleachfuel Thanks a lot for the review, I addressed your comments :) |
.into_compile_error() | ||
.into(); | ||
ast.span(), | ||
"Custom on_insert hooks are not supported as relationships already define an on_insert hook", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future work: we can probably automatically concatenate these function calls 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs doc tests, both to ensure that this works and teach users that it does. Code looks great, and the feature is 🔥 though.
For things that should fail to compile, there's a bevy_ecs_compile_fail
crate that you can add stuff to :)
@cart, this steps on the toes of your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! I have a small wording suggestion but those doc tests should be sufficient to validate that this keeps working.
I think you could probably be a bit more clear in your compile fail tests about why the code shouldn't compile but I won't block on it.
@alice-i-cecile note that the compile tests weren't evaluated nor updated by me so please don't merge yet. I don't have rustup installed and this kind of blocks me here. I'm looking into it though. |
The compile tests are fixed and I was able to generate the new stderr files |
crates/bevy_ecs/compile_fail/tests/ui/component_hook_relationship.rs
Outdated
Show resolved
Hide resolved
crates/bevy_ecs/compile_fail/tests/ui/component_hook_closure_type_mismatch.rs
Outdated
Show resolved
Hide resolved
@RobWalt I've removed the 0.16 milestone from this since I don't think it's pressing enough to block the release, but this work is free to proceed. Do bother folks for more reviews once you've removed the in-line function syntax. |
Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke! If it's expected, please add the S-Deliberate-Rendering-Change label. |
I noticed this while working on #18017 . Some of the `stderr` compile_fail tests were updated while I generated the output for the new tests I introduced in the mentioned PR. I'm on rust 1.85.0
This commit adds: - function call hook attributes - main feature of this commit This allows to reuse common functionality without replicating a lot of boilerplate. A small example is a hook which just adds different default sprites. The sprite loading code would be the same for every component. Unfortunately we can't use the required components feature, since we need at least an `AssetServer` or other `Resource`s or `Component`s to load the sprite. ```rs fn load_sprite(path: &str) -> impl Fn(DeferredWorld, HookContext) { |mut world, ctx| { // ... use world to load sprite } } \#[derive(Component)] \#[component(on_add = load_sprite("knight.png"))] struct Knight; \#[derive(Component)] \#[component(on_add = load_sprite("monster.png"))] struct Monster; ```
55a0c4f
to
79df196
Compare
@RobWalt I think the recent macro cleanup stuff broke the imports here (or maybe my merge conflict resolution was bad?). Either way, ping me when this is green and I'll press the button :) |
Objective
This PR adds:
#[component(on_add = func(42))]
#[component(on_add = |w, ctx| { /* ... */ })]
This allows to reuse common functionality without replicating a lot of boilerplate. A small example is a hook which just adds different default sprites. The sprite loading code would be the same for every component. Unfortunately we can't use the required components feature, since we need at least an
AssetServer
or otherResource
s orComponent
s to load the sprite.The commit also reorders the logic of the derive macro a bit. It's probably a bit less lazy now, but the functionality shouldn't be performance critical and is executed at compile time anyways.
Solution
HookKind
enum in the component proc macro moduleTesting
I have some code laying around. I'm not sure where to put it yet though. Also is there a way to check compilation failures? Anyways, here it is:
Showcase
I'll try to continue to work on this to have a small section in the release notes.