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 some core::cmp::Ordering helpers #79656

Merged
merged 1 commit into from
Dec 11, 2020
Merged

Add some core::cmp::Ordering helpers #79656

merged 1 commit into from
Dec 11, 2020

Conversation

jnqnfe
Copy link
Contributor

@jnqnfe jnqnfe commented Dec 3, 2020

...to allow easier equal-to-or-greater-than and less-than-or-equal-to
comparisons.

Prior to Rust 1.42 a greater-than-or-equal-to comparison might be written
either as a match block, or a traditional conditional check like this:

if cmp == Ordering::Equal || cmp == Ordering::Greater {
    // Do something
}

Which requires two instances of cmp. Don't forget that while cmp here
is very short, it could be something much longer in real use cases.

From Rust 1.42 a nicer alternative is possible:

if matches!(cmp, Ordering::Equal | Ordering::Greater) {
    // Do something
}

The commit adds another alternative which may be even better in some cases:

if cmp.is_equal_or_greater() {
    // Do something
}

The earlier examples could be cleaner than they are if the variants of
Ordering are imported such that Equal, Greater and Less can be
referred to directly, but not everyone will want to do that.

The new solution can shorten lines, help avoid logic mistakes, and avoids
having to import Ordering / Ordering::*.

@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 3, 2020
@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 3, 2020

If is_equal(), is_greater() and is_less() helpers are wanted also, I'm happy to add incorporate those too.

@egilburg
Copy link

egilburg commented Dec 3, 2020

Bikeshedding: Common word syntax for such matchers is eq, ne / neq, lt, lte/lteq, gt, gte/gteq. Perhaps add them as such?

@sfackler
Copy link
Member

sfackler commented Dec 4, 2020

I agree that is_lt, is_le, is_eq, etc make sense to match the PartialOrd method names.

library/core/src/cmp.rs Outdated Show resolved Hide resolved
@jnqnfe jnqnfe force-pushed the ordering branch 3 times, most recently from d6fcc7c to b03d04b Compare December 4, 2020 04:47
@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 4, 2020

Ok, I've revised things, including addressing the feedback.

  • Added the additional three helpers mentioned along with a forth (not-equal).
  • Adjusted to the shorter names suggested. (is_eq, is_ne, is_lt, is_gt, is_le, is_ge).
  • Moved them to the start of the set of methods for the type, since they are more fundamental than those that already exist.
  • Revised the commit log.

I force-pushed the changes to keep things tidy, since it's such a trivial one to review.

@jnqnfe jnqnfe requested a review from scottmcm December 9, 2020 22:16
@scottmcm
Copy link
Member

scottmcm commented Dec 9, 2020

@jnqnfe Looks like this is still failing CI?

@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 9, 2020

@jnqnfe Looks like this is still failing CI?

@scottmcm Yeah, the failure is stating that it 'could not download the log' and to try again later... I see no button to ask it to try again...

@scottmcm
Copy link
Member

scottmcm commented Dec 9, 2020

You have a bunch of these:
image

It'll probably have to use matches! for all of them.

@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2020
@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 9, 2020

You have a bunch of these:
[snip]
It'll probably have to use matches! for all of them.

Ah, thanks, fixed! (Sorry about that, it's been a while since I last contributed and I haven't been able to get things setup for compiling this time, partly because I'm using the stable compiler version from my repo :/ )

I've force-pushed that fix, fingers crossed CI will work this time :)

Oh, result came in before I could post, it still can't get the log...

@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 9, 2020

@scottmcm Ohhhhh, I've just noticed that a github domain was being blocked by noscript in my browser and as a consequence the errors you saw were being hidden from me. I was under the impression that there was some issue with CI itself because all I saw was the 'could not download log' error line. Sorry about that, I would have fixed the errors days ago if I'd realised.

I've just fixed the typo in the last push, so should be good now.

Okay, so now it's complaining about missing stability attributes...

@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 10, 2020

@scottmcm Looking into what stability attribute to assign, I'm not at all certain.

  • Do I need to go through the effort of creating a new unique feature gate for such a simple addition as this? Or can I use something like std_misc as the feature?
  • Can I mark this as stable (for 1.50.0?) immediately, or do you want it marked unstable?
  • If marking unstable, must I then create a tracking issue for this or is it acceptable to set the issue attribute to "none"?

@scottmcm
Copy link
Member

See https://rustc-dev-guide.rust-lang.org/stability.html#unstable

Yes, you need a feature name and a tracking issue. Just invent a reasonable name; you don't need to put it anywhere other than in the attributes.

Whenever possible -- as it is here -- things go in unstable. That allows them to go in with just a PR sign-off, whereas stabilization will require a full libs team FCP.

@jnqnfe jnqnfe force-pushed the ordering branch 3 times, most recently from b4f9169 to 1016655 Compare December 10, 2020 07:33
@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 10, 2020

@scottmcm Ok. I've:

  • Created the tracking issue.
  • Created a new feature ordering_helpers in rustc_features (that guide btw needs updating to note that this exists under compiler/).
  • Created an unstable book entry, similar to others.
  • Tagged the new methods with the appropriate attribute.
  • Added a "sym" entry in rustc_span, following a CI error and guessing from other merged work. (This was not mentioned at all in the contributing guide).
  • Added a test, making a complete guess at where from copying other merged work.

Outstanding:

  • Filling in the stderr file for the test. Since I don't have the nightly compiler I can't compile to get the text to fill it in with. Can you please help with this.

I merged the changes into a single commit. I'll split things up if preferred. Thanks for your patience.

@scottmcm
Copy link
Member

Sorry, I guess I wasn't clear here. The "you don't need to put it anywhere other than in the attributes" I said above is because you don't need the compiler or unstable_book changes or the feature gate UI test.

See https://github.com/rust-lang/rust/pull/79673/files for an example of adding unstable methods.

@jnqnfe
Copy link
Contributor Author

jnqnfe commented Dec 10, 2020

Oh ok, thanks, updated.

CI has passed now finally :D

library/core/src/cmp.rs Outdated Show resolved Hide resolved
...to allow easier greater-than-or-equal-to and less-than-or-equal-to
comparisons, and variant checking without needing to import the enum,
similar to `Option::is_none()` / `Option::is_some()`, in situations where
you are dealing with an `Ordering` value. (Simple `PartialOrd` / `Ord`
based evaluation may not be suitable for all situations).

Prior to Rust 1.42 a greater-than-or-equal-to comparison might be written
either as a match block, or a traditional conditional check like this:

```rust
if cmp == Ordering::Equal || cmp == Ordering::Greater {
    // Do something
}
```

Which requires two instances of `cmp`. Don't forget that while `cmp` here
is very short, it could be something much longer in real use cases.

From Rust 1.42 a nicer alternative is possible:

```rust
if matches!(cmp, Ordering::Equal | Ordering::Greater) {
    // Do something
}
```

The commit adds another alternative which may be even better in some cases:

```rust
if cmp.is_ge() {
    // Do something
}
```

The earlier examples could be cleaner than they are if the variants of
`Ordering` are imported such that `Equal`, `Greater` and `Less` can be
referred to directly, but not everyone will want to do that.

The new solution can shorten lines, help avoid logic mistakes, and avoids
having to import `Ordering` / `Ordering::*`.
@sfackler
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Dec 10, 2020

📌 Commit 169c59f has been approved by sfackler

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 10, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 10, 2020
Add some core::cmp::Ordering helpers

...to allow easier equal-to-or-greater-than and less-than-or-equal-to
comparisons.

Prior to Rust 1.42 a greater-than-or-equal-to comparison might be written
either as a match block, or a traditional conditional check like this:

```rust
if cmp == Ordering::Equal || cmp == Ordering::Greater {
    // Do something
}
```

Which requires two instances of `cmp`. Don't forget that while `cmp` here
is very short, it could be something much longer in real use cases.

From Rust 1.42 a nicer alternative is possible:

```rust
if matches!(cmp, Ordering::Equal | Ordering::Greater) {
    // Do something
}
```

The commit adds another alternative which may be even better in some cases:

```rust
if cmp.is_equal_or_greater() {
    // Do something
}
```

The earlier examples could be cleaner than they are if the variants of
`Ordering` are imported such that `Equal`, `Greater` and `Less` can be
referred to directly, but not everyone will want to do that.

The new solution can shorten lines, help avoid logic mistakes, and avoids
having to import `Ordering` / `Ordering::*`.
@bors
Copy link
Contributor

bors commented Dec 11, 2020

⌛ Testing commit 169c59f with merge 0c9ef56...

@bors
Copy link
Contributor

bors commented Dec 11, 2020

☀️ Test successful - checks-actions
Approved by: sfackler
Pushing 0c9ef56 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 11, 2020
@bors bors merged commit 0c9ef56 into rust-lang:master Dec 11, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 11, 2020
@jnqnfe jnqnfe deleted the ordering branch December 11, 2020 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants