-
-
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
[Merged by Bors] - Make get_resource (and friends) infallible #4047
[Merged by Bors] - Make get_resource (and friends) infallible #4047
Conversation
Note to reviewers: all of the non-trivial changes are found in World::mod.res. The rest is just fallout from this change. |
Haven't reviewed the code, but this change seems sensible. However, the |
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 some small nits. Very lovely change!
Co-authored-by: KDecay <KDecayMusic@protonmail.com>
Not sure what the most idiomatic way to do this would be, but IMO
|
Yeah, that would be consistent with the |
To add to the bikeshedding, I feel like |
To further add to the bikeshedding, I agree and feel this is the common rust convention, e.g. used in std. I would expect |
Great, I'll make this change. This also ensures that this change is non-breaking, which is very nice. |
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.
💯 for this quality of life improvement PR
Co-authored-by: MinerSebas <66798382+MinerSebas@users.noreply.github.com>
bors r+ |
# Objective - In the large majority of cases, users were calling `.unwrap()` immediately after `.get_resource`. - Attempting to add more helpful error messages here resulted in endless manual boilerplate (see #3899 and the linked PRs). ## Solution - Add an infallible variant named `.resource` and so on. - Use these infallible variants over `.get_resource().unwrap()` across the code base. ## Notes I did not provide equivalent methods on `WorldCell`, in favor of removing it entirely in #3939. ## Migration Guide Infallible variants of `.get_resource` have been added that implicitly panic, rather than needing to be unwrapped. Replace `world.get_resource::<Foo>().unwrap()` with `world.resource::<Foo>()`. ## Impact - `.unwrap` search results before: 1084 - `.unwrap` search results after: 942 - internal `unwrap_or_else` calls added: 4 - trivial unwrap calls removed from tests and code: 146 - uses of the new `try_get_resource` API: 11 - percentage of the time the unwrapping API was used internally: 93%
- In the large majority of cases, users were calling `.unwrap()` immediately after `.get_resource`. - Attempting to add more helpful error messages here resulted in endless manual boilerplate (see bevyengine#3899 and the linked PRs). - Add an infallible variant named `.resource` and so on. - Use these infallible variants over `.get_resource().unwrap()` across the code base. I did not provide equivalent methods on `WorldCell`, in favor of removing it entirely in bevyengine#3939. Infallible variants of `.get_resource` have been added that implicitly panic, rather than needing to be unwrapped. Replace `world.get_resource::<Foo>().unwrap()` with `world.resource::<Foo>()`. - `.unwrap` search results before: 1084 - `.unwrap` search results after: 942 - internal `unwrap_or_else` calls added: 4 - trivial unwrap calls removed from tests and code: 146 - uses of the new `try_get_resource` API: 11 - percentage of the time the unwrapping API was used internally: 93%
Objective
.unwrap()
immediately after.get_resource
.unwrap
withexpect
#3899 and the linked PRs).Solution
.resource
and so on..get_resource().unwrap()
across the code base.Notes
I did not provide equivalent methods on
WorldCell
, in favor of removing it entirely in #3939.Migration Guide
Infallible variants of
.get_resource
have been added that implicitly panic, rather than needing to be unwrapped.Replace
world.get_resource::<Foo>().unwrap()
withworld.resource::<Foo>()
.Impact
.unwrap
search results before: 1084.unwrap
search results after: 942unwrap_or_else
calls added: 4try_get_resource
API: 11