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

Implement std::ops::Not for ShouldRun #2444

Closed
wants to merge 2 commits into from
Closed

Implement std::ops::Not for ShouldRun #2444

wants to merge 2 commits into from

Conversation

tsoutsman
Copy link
Contributor

Objective

  • Add the negation operator to ShouldRun

Reason

Currently, two functions are required when using binary run criteria, like in this example from the unofficial cheat book. The negation operator allows you to perform the task with just one function, reducing duplicate code and logic.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jul 9, 2021
@mockersf
Copy link
Member

mockersf commented Jul 9, 2021

Could you go on #2373 and mention if you're ok with the license change?

@mockersf mockersf added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed S-Needs-Triage This issue needs to be labelled labels Jul 9, 2021
@tsoutsman
Copy link
Contributor Author

Done

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.

Actual implementation looks good, I'm just less sure about whether this should use the Not operator.

@@ -44,6 +44,19 @@ pub enum ShouldRun {
NoAndCheckAgain,
}

impl std::ops::Not for ShouldRun {
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 certain that this should be the Not operator. I think an associated function called 'invert' or something might make more sense.

(Also I don't think rust-analyzer jumps to Not implementations yet (requires at least rust-lang/rust-analyzer#4558 iirc), so the weird factor is quite high and it's not very inspectible)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could it have both and then one just calls another?

@Ratysz
Copy link
Contributor

Ratysz commented Jul 10, 2021

The negation operator allows you to perform the task with just one function, reducing duplicate code and logic.

Can you please elaborate on that? I don't see how this syntax would affect the linked example.

@tsoutsman
Copy link
Contributor Author

tsoutsman commented Jul 10, 2021

Can you please elaborate on that? I don't see how this syntax would affect the linked example.

Instead of:

fn run_if_connected(
    mode: Res<MultiplayerKind>,
    session: Res<MyNetworkSession>,
) -> ShouldRun
{
    if *mode == MultiplayerKind::Client && session.is_connected() {
        ShouldRun::Yes
    } else {
        ShouldRun::No
    }
}

it could just be:

fn run_if_connected(
    mode: Res<MultiplayerKind>,
    session: Res<MyNetworkSession>,
) -> ShouldRun
{
    !run_if_host(mode)
}

which contains the logic in just one function instead of duplicating it in two. It's not that big of an improvement, but it would make more of a difference for bigger systems. It also makes it clear that this is the exact opposite of the other function.

@Ratysz
Copy link
Contributor

Ratysz commented Jul 10, 2021

I see now. Still, there is a problem with applying binary operators to non-binary values: lack of clarity with regards to which specific states are being operated on. As in, in a different context the opposite of NoAndCheckAgain is No, and there is no way to tell whether it is !No == NoAndCheckAgain or !No == Yes without looking at the source code.

I don't think that being explicit even in an example this small is a loss:

fn run_if_connected(
    mode: Res<MultiplayerKind>,
    session: Res<MyNetworkSession>,
) -> ShouldRun {
    match run_if_host(mode) {
        Yes => No,
        No => Yes,
        _ => unreachable!(),
    }
}

If this comes up a lot in a project, factor the inversion out into a function; an operator is too ambiguous to be included by default.

I should clarify: I'm going by your last message here. In the linked example run_if_connected() and run_if_host() are not negatives of each other.

@tsoutsman
Copy link
Contributor Author

I see now. Still, there is a problem with applying binary operators to non-binary values: lack of clarity with regards to which specific states are being operated on. As in, in a different context the opposite of NoAndCheckAgain is No, and there is no way to tell whether it is !No == NoAndCheckAgain or !No == Yes without looking at the source code.

Implementing @DJMcNab's suggestion of an invert method would make it clearer, although I agree that this could very easily be implemented in a project. Then there wouldn't be any confusion or potential for misinterpretation.

I should clarify: I'm going by your last message here. In the linked example run_if_connected() and run_if_host() are not negatives of each other.

My bad, I probably should have properly read the example, but you can imagine a similar example where they are negatives.

@cart cart added the S-Pre-Relicense This PR was made before Bevy added the Apache license. Cannot be merged or used for other work label Jul 23, 2021
@mockersf mockersf removed the S-Pre-Relicense This PR was made before Bevy added the Apache license. Cannot be merged or used for other work label Jul 24, 2021
@alice-i-cecile
Copy link
Member

As much as I want this, I don't think it's feasible unless we swap to a binary logic for ShouldRun. I'm going to close this out; we'll reimplement this ASAP if and when that happens <3

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants