about summary refs log tree commit diff
path: root/compiler/rustc_codegen_cranelift/src/optimize/code_layout.rs
diff options
context:
space:
mode:
authorLyndon Brown <jnqnfe@gmail.com>2020-12-10 07:00:17 +0000
committerLyndon Brown <jnqnfe@gmail.com>2020-12-10 20:32:12 +0000
commit169c59ff0fcbd606c6f639c2318f71b51f6c9207 (patch)
treecc8c4d3f2a25bf3fdb1fbf5a25328d4ac4677ce7 /compiler/rustc_codegen_cranelift/src/optimize/code_layout.rs
parente413d89aa706060ddc347e1e06d551ec86d3f471 (diff)
downloadrust-169c59ff0fcbd606c6f639c2318f71b51f6c9207.tar.gz
rust-169c59ff0fcbd606c6f639c2318f71b51f6c9207.zip
Add some core::cmp::Ordering helpers
...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::*`.
Diffstat (limited to 'compiler/rustc_codegen_cranelift/src/optimize/code_layout.rs')
0 files changed, 0 insertions, 0 deletions