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

[Merged by Bors] - impl SystemParam for Option<Res<T>> / Option<ResMut<T>> #1494

Closed
wants to merge 2 commits into from
Closed

Conversation

inodentry
Copy link
Contributor

This allows users to write systems that do not panic if a resource does not exist at runtime (such as if it has not been inserted yet).

This is a copy-paste of the impls for Res and ResMut, with an extra check to see if the resource exists.

There might be a cleaner way to do it than this check. I don't know.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use labels Feb 21, 2021
Copy link
Member

@TheRawMeatball TheRawMeatball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I really like this

Comment on lines 246 to 532
if resources.contains::<T>() {
Some(Some(Res::new(
resources.get_unsafe_ref::<T>(ResourceIndex::Global),
)))
} else {
Some(None)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is cleaner or not, but it's worth considering

Suggested change
if resources.contains::<T>() {
Some(Some(Res::new(
resources.get_unsafe_ref::<T>(ResourceIndex::Global),
)))
} else {
Some(None)
}
Some(
resources
.contains::<T>()
.then(|| Res::new(resources.get_unsafe_ref::<T>(ResourceIndex::Global))),
)

Copy link
Contributor Author

@inodentry inodentry Feb 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the imperative style over the functional style.

So i think the original is clearer and more readable.

(and probably also easier for the compiler)

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 21, 2021
@alice-i-cecile
Copy link
Member

I think this needs an example and a mention in the Res / ResMut docstrings. I really like this functionality though!

@inodentry
Copy link
Contributor Author

Added doc comments.

I can't come up with a meaningful way to add it to the ecs_guide example right now.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful functionality to expse.

Also this will almost certainly need to be changed for the ecs pr when that arrives, since the SystemParam trait will be changing.

_world: &'a World,
resources: &'a Resources,
) -> Option<Self::Item> {
if resources.contains::<T>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This double lookup is not good - ideally we'd just have a get_unsafe_ref which returns an Option<NonNull<T>>.

(I think the most likely response from cart will be to point this out)

@cart
Copy link
Member

cart commented Mar 3, 2021

I'm in favor of these changes. Lets just update this when #1525 is merged.

@inodentry
Copy link
Contributor Author

Updated for ECS v2.

This time, thanks to the functionality being split across 2 separate traits, I could implement it more cleanly by wrapping an underlying ResState and delegating to it, rather than copypasting the trait impl code.

There is also no extra check, because the resource fetch API returns an Option now, so we can just map that.

@cart
Copy link
Member

cart commented Mar 8, 2021

bors r+

bors bot pushed a commit that referenced this pull request Mar 8, 2021
This allows users to write systems that do not panic if a resource does not exist at runtime (such as if it has not been inserted yet).

This is a copy-paste of the impls for `Res` and `ResMut`, with an extra check to see if the resource exists.

There might be a cleaner way to do it than this check. I don't know.
@bors
Copy link
Contributor

bors bot commented Mar 8, 2021

@bors bors bot changed the title impl SystemParam for Option<Res<T>> / Option<ResMut<T>> [Merged by Bors] - impl SystemParam for Option<Res<T>> / Option<ResMut<T>> Mar 8, 2021
@bors bors bot closed this Mar 8, 2021
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 simple quality-of-life change that makes Bevy easier to use 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.

5 participants