-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat: Add World::get_reflect()
and World::get_reflect_mut()
#14416
feat: Add World::get_reflect()
and World::get_reflect_mut()
#14416
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
Open question: I think these might also make sense for |
Question: Would it make sense to add something similar for |
Also, just realized that relying on A possible alternative would be to extend the arguments of |
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.
generally very happy with this succinct and useful PR. it definitely could be merged as it is, i've just left a few comments/questions!
i reviewed this on my phone so the comment quality might not be great.
regardless, the bulk of the changes might be better off in their own file under the bevy_ecs::reflect
module since the file there currently in is already exorbitantly long!
crates/bevy_ecs/src/world/mod.rs
Outdated
assert_eq!( | ||
reflect_from_ptr.type_id(), | ||
type_id, | ||
"Mismatch between Ptr's type_id and ReflectFromPtr's type_id", | ||
); |
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.
i would definitely move this assertion to above the unsafe block. it's a precondition for e unsafe code, which should be as minimal as possible.
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.
I actually thought of that and tried it out, but I didn't like it, because it felt like it "decouples" the assertion too much from the unsafe block. I.e., copying/moving the unsafe block should also copy/move the assertion with it, which automatically happens if the assertion is inside the unsafe block, but can easily be forgotten if it's outside.
That said, I can also just go with the common style in bevy, and if that is to keep assertions outside, I'll just change it.
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.
Since the assertion is actually required for soundness of the unsafe
-block, I'd prefer to keep it inside, also for the reasons from my previous comment. Would that be ok?
@futile ping me when you've addressed review comments (or if you're happy enough with the PR for now) and I'll give this the second review you need :) |
Thank you for the review! :) You mean like in a trait, e.g., |
it's even simpler than using an extension trait. since it's in the same crate you can write an inherent impl ( |
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.
Awesome! I think this will be a very useful API since people tend to ask how they can convert TypeId
to a dyn Reflect
component reference.
I agree with @soqb though, it will probably be cleaner to move these methods to a separate module.
crates/bevy_ecs/src/world/error.rs
Outdated
/// | ||
/// Did you call [`App::register_type()`]? | ||
/// | ||
/// [`App::register_type()`]: ../../../bevy_app/struct.App.html#method.register_type |
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.
You should be able to link to the method like:
/// [`App::register_type`]: crate::app::App::register_type
Note that I believe we tend to leave off the ()
when referencing functions/methods.
Same for the MissingReflectFromPtrTypeData
variant.
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.
Removed the ()
(everywhere I could find).
So, about the linking for App::register_type
.. I started out with something like crate::app::App::register_type
, but that doesn't work:
warning: unresolved link to `crate::app::App::register_type`
--> crates/bevy_ecs/src/world/error.rs:29:33
|
29 | /// [`App::register_type`]: crate::app::App::register_type
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `app` in module `bevy_ecs`
I think the problem is that bevy_ecs
does not depend on bevy_app
(but the other way around), so rustdoc has no chance to find App
. That's why I went with the manual file-path link.
That said, I just tried out crate::app::App::register_type
again, because I really would like a better solution for this, but it doesn't work, with the error given above.
Is it possible to declare like a "doc-dependency" on bevy_app
, without introducing a cycle? 😅 I think not, but I feel like it could work in theory, but probably not in rustdoc/cargo doc for now.
Edit: Found an upstream issue for this: rust-lang/rust#74481
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.
Oh that makes sense. In that case I might suggest linking to TypeRegistry::register
directly or just not making App::register_type
a link at all. I don't know I we ever link to crate dependents with a relative rustdoc path (I also don't know if that works for docs.rs docs).
@alice-i-cecile any thoughts here?
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.
I guess we can check what's gonna happen on docs.rs? Shouldn't break anything anyways.
@MrGVSV thank you for your review as well! :) I think I should have addressed all of it, and also moved the new methods + error type to a new module I left most of the comments I took care of with a "done"-comment or similar instead of closing them, because I feel like (only) the author of a comment can decide whether it's actually done :) Is there a common practice for this in the bevy repo that I should follow? Oh yeah, open question from before: Is it fine to depend on |
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
c81d383
to
0cea40b
Compare
Not particularly. For trivial / boring fixes (typos, code style), I tend to resolve it as an author or third-party reviewer. For particularly complex stuff, or where there's disagreement, I leave it open until the OP comes by. For particularly interesting threads, I leave it open even after it's fully resolved to help teach folks. |
Ah ok, yeah, makes sense. In that case I went ahead and resolved a few more issues, where the solution should be pretty obvious & expected. Thanks! :) |
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 a couple cleanup suggestions. Everything else looks good!
Makes sense to me!
Let's stick to using |
Got it, fixed them. Nice catches, ty!
Sounds good to me as well 👍 |
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 docs, great error handling, useful bit of sugar. Nice work y'all :D
Objective
Sometimes one wants to retrieve a
&dyn Reflect
for an entity's component, which so far required multiple, non-obvious steps andunsafe
-code.The docs for
MutUntyped
contain an example of the unsafe part.Solution
This PR adds the two methods:
which take care of the necessary steps, check required invariants etc., and contain the unsafety so the caller doesn't have to deal with it.
Testing
cargo run -p ci
.cargo test --doc --package bevy_ecs --all-features -- world::World::get_reflect --show-output
for the doctestcargo test --package bevy_ecs --lib --all-features -- world::tests::reflect_tests --show-output
for the unittestsShowcase
Copy of the doctest example which gives a good overview of what this enables:
Migration Guide
No breaking changes, but users can use the new methods if they did it manually before.