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 Clone to common conditions #8060

Merged
merged 6 commits into from
Mar 13, 2023
Merged

add Clone to common conditions #8060

merged 6 commits into from
Mar 13, 2023

Conversation

B-Reif
Copy link
Contributor

@B-Reif B-Reif commented Mar 12, 2023

Objective

The system configuration distributive_run_if requires the given condition to be Clone. This doesn't currently work with the closures returned by common_conditions modules. Partially fixes #8058.

Solution

Add Clone to the return type of common conditions wherever possible.

Caveats

  • Because the common conditions resource_equals and resource_exists_and_equals have to dereference the resource they're comparing, these functions would have to specify the resource type to be T: Clone if we want the returned closure to also be Clone.
  • The not condition takes any arbitrary condition which may or may not be Clone, and the returned CombinatorSystem isn't Clone.

I'm not sure how to support Clone in these cases. This would likely require some new traits? Open to ideas on this.


Changelog

Add Clone to the return type of most common run conditions.

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Mar 12, 2023
@alice-i-cecile alice-i-cecile added this to the 0.10.1 milestone Mar 12, 2023
@alice-i-cecile
Copy link
Member

Strongly in favor of this: let's keep this simple and nonbreaking for now so we can ship it in the point release. Can you add a few tests to assert that distributive_run_if compiles with these common conditions?

@B-Reif
Copy link
Contributor Author

B-Reif commented Mar 12, 2023

Strongly in favor of this: let's keep this simple and nonbreaking for now so we can ship it in the point release. Can you add a few tests to assert that distributive_run_if compiles with these common conditions?

Sure, where should they go?

@alice-i-cecile
Copy link
Member

I would add them in a tests module at the end of each of these files.

@B-Reif
Copy link
Contributor Author

B-Reif commented Mar 12, 2023

Oh that probably makes more sense than what I just did, I added a test to distributive_run_if. I'll change it around.

@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 Mar 12, 2023
Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

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

Good change.

This doesn't have to be done now, but here's how we can support Clone for not:

  1. Implement Clone for CombinatorSystem when both composite systems are Clone.
  2. Instead of returning impl Condition<> from not, return a concrete type (probably a type alias). Then the compiler knows that the returned system is Clone as long as the input system is.

@@ -142,7 +142,7 @@ pub mod common_conditions {

/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
/// if the first time the condition is run and false every time after
pub fn run_once() -> impl FnMut() -> bool {
pub fn run_once() -> impl Clone + FnMut() -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

readability nit: FnMut is more important here than Clone so IMO it should come first.

Copy link
Member

Choose a reason for hiding this comment

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

FnMut() -> bool + Clone looks weird to me. It makes it look like the function is returning bool + Clone somehow.

@B-Reif
Copy link
Contributor Author

B-Reif commented Mar 13, 2023

Good change.

This doesn't have to be done now, but here's how we can support Clone for not:

1. Implement `Clone` for `CombinatorSystem` when both composite systems are `Clone`.

2. Instead of returning `impl Condition<>` from `not`, return a concrete type (probably a type alias). Then the compiler knows that the returned system is `Clone` as long as the input system is.

How can we get the concrete type for the closure that does the inversion?

@JoJoJet
Copy link
Member

JoJoJet commented Mar 13, 2023

Ah, I left out a step. You'd have to replace .pipe() with CombinatorSystem using a custom Combine impl.

@alice-i-cecile
Copy link
Member

@B-Reif unless I hear otherwise I'm going to merge this PR in about 10 hours :) We can add the additional fixes in a follow-up.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 13, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Mar 13, 2023
@B-Reif
Copy link
Contributor Author

B-Reif commented Mar 13, 2023

@alice-i-cecile Just fixed the merge conflict, please try again :)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 13, 2023
Merged via the queue into bevyengine:main with commit 7c4a0eb Mar 13, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
mockersf pushed a commit that referenced this pull request Mar 27, 2023
@ilyvion
Copy link
Contributor

ilyvion commented Jul 26, 2023

I take it that because those commits 👆 happened after the PR was merged, they didn't actually make it in? I'm using bevy 0.11 right now, and resource_equals is still not Clone, e.g., even though the resource I'm trying to use it with definitely is.

@JoJoJet
Copy link
Member

JoJoJet commented Jul 27, 2023

In order for resource_equals to be clone, the return type would have to be something like impl FnMut(Res<T>) -> bool + Clone if T: Clone. Rust's type system isn't advanced enough to express a conditional trait impl like that, so resource_equals is just never Clone, unfortunately.

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 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.

Unable to use distributive_run_if with common conditions
6 participants