diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-09-18 18:27:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-18 18:27:18 +0200 |
| commit | db9e217989b4d91dfac6050b436c8d657846a0e4 (patch) | |
| tree | 238ae5e372b43b28507cb92df79df4be2ea8ff5f /compiler/rustc_interface/src/errors.rs | |
| parent | cbcf9a5368c0d8b6d0b5784201471475cb3496a3 (diff) | |
| parent | 0c3e0abbf8cff8cf0765c41f8d49000bc467d642 (diff) | |
| download | rust-db9e217989b4d91dfac6050b436c8d657846a0e4.tar.gz rust-db9e217989b4d91dfac6050b436c8d657846a0e4.zip | |
Rollup merge of #109409 - WaffleLapkin:progamer, r=dtolnay
Add `minmax{,_by,_by_key}` functions to `core::cmp`
This PR adds the following functions:
```rust
// mod core::cmp
#![unstable(feature = "cmp_minmax")]
pub fn minmax<T>(v1: T, v2: T) -> [T; 2]
where
T: Ord;
pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
where
F: FnOnce(&T, &T) -> Ordering;
pub fn minmax_by_key<T, F, K>(v1: T, v2: T, mut f: F) -> [T; 2]
where
F: FnMut(&T) -> K,
K: Ord;
```
(they are also `const` under `#[feature(const_cmp)]`, I've omitted `const` stuff for simplicity/readability)
----
Semantically these functions are equivalent to `{ let mut arr = [v1, v2]; arr.sort(); arr }`, but since they operate on 2 elements only, they are implemented as a single comparison.
Even though that's basically a sort, I think "sort 2 elements" operation is useful on it's own in many cases. Namely, it's a common pattern when you have 2 things, and need to know which one is smaller/bigger to operate on them differently.
I've wanted such functions countless times, most recently in #109402, so I thought I'd propose them.
----
r? libs-api
Diffstat (limited to 'compiler/rustc_interface/src/errors.rs')
0 files changed, 0 insertions, 0 deletions
