[Merged by Bors] - Make get_resource (and friends) infallible#4047
[Merged by Bors] - Make get_resource (and friends) infallible#4047alice-i-cecile wants to merge 17 commits intobevyengine:mainfrom
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 |
ghost
left a comment
There was a problem hiding this comment.
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. |
mcobzarenco
left a comment
There was a problem hiding this comment.
💯 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.unwrapwithexpect#3899 and the linked PRs).Solution
.resourceand 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_resourcehave been added that implicitly panic, rather than needing to be unwrapped.Replace
world.get_resource::<Foo>().unwrap()withworld.resource::<Foo>().Impact
.unwrapsearch results before: 1084.unwrapsearch results after: 942unwrap_or_elsecalls added: 4try_get_resourceAPI: 11