-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Conversation
Could you go on #2373 and mention if you're ok with the license change? |
Done |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
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. |
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 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 |
Implementing @DJMcNab's suggestion of an
My bad, I probably should have properly read the example, but you can imagine a similar example where they are negatives. |
As much as I want this, I don't think it's feasible unless we swap to a binary logic for |
Objective
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.