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

Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with #15758

Merged
merged 3 commits into from
Oct 9, 2024

Conversation

ItsDoot
Copy link
Contributor

@ItsDoot ItsDoot commented Oct 9, 2024

Objective

If a Resource implements FromWorld or Default, it's nicer to be able to write:

let foo = world.get_resource_or_init::<Foo>();

Rather than:

let foo = world.get_resource_or_insert_with(Foo::default);

The latter is also not possible if a type implements FromWorld only, and not Default.

Solution

Added:

impl World {
    pub fn get_resource_or_init<R: Resource + FromWorld>(&mut self) -> Mut<'_, R>;
}

Turns out all current in-engine uses of get_resource_or_insert_with are exactly the above, so they've also been replaced.

Testing

  • Added a doc-test.
  • Also added a doc-test for World::get_resource_or_insert_with.

@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 9, 2024
@pablo-lua pablo-lua added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 9, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 9, 2024
Merged via the queue into bevyengine:main with commit b4071ca Oct 9, 2024
33 checks passed
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 targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants