Skip to content

Commit acfe959

Browse files
authored
Auto merge of rust-lang#37054 - rednum:master, r=alexcrichton
Add or and or_else for ordering. Fixes rust-lang#37053 (see discussion in rust-lang/rfcs#1677).
2 parents 35a1fef + 655effe commit acfe959

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/libcore/cmp.rs

+74
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,80 @@ impl Ordering {
248248
Greater => Less,
249249
}
250250
}
251+
252+
/// Chains two orderings.
253+
///
254+
/// Returns `self` when it's not `Equal`. Otherwise returns `other`.
255+
/// # Examples
256+
///
257+
/// ```
258+
/// #![feature(ordering_chaining)]
259+
///
260+
/// use std::cmp::Ordering;
261+
///
262+
/// let result = Ordering::Equal.then(Ordering::Less);
263+
/// assert_eq!(result, Ordering::Less);
264+
///
265+
/// let result = Ordering::Less.then(Ordering::Equal);
266+
/// assert_eq!(result, Ordering::Less);
267+
///
268+
/// let result = Ordering::Less.then(Ordering::Greater);
269+
/// assert_eq!(result, Ordering::Less);
270+
///
271+
/// let result = Ordering::Equal.then(Ordering::Equal);
272+
/// assert_eq!(result, Ordering::Equal);
273+
///
274+
/// let x: (i64, i64, i64) = (1, 2, 7);
275+
/// let y: (i64, i64, i64) = (1, 5, 3);
276+
/// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
277+
///
278+
/// assert_eq!(result, Ordering::Less);
279+
/// ```
280+
#[unstable(feature = "ordering_chaining", issue = "37053")]
281+
pub fn then(self, other: Ordering) -> Ordering {
282+
match self {
283+
Equal => other,
284+
_ => self,
285+
}
286+
}
287+
288+
/// Chains the ordering with the given function.
289+
///
290+
/// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
291+
/// the result.
292+
///
293+
/// # Examples
294+
///
295+
/// ```
296+
/// #![feature(ordering_chaining)]
297+
///
298+
/// use std::cmp::Ordering;
299+
///
300+
/// let result = Ordering::Equal.then_with(|| Ordering::Less);
301+
/// assert_eq!(result, Ordering::Less);
302+
///
303+
/// let result = Ordering::Less.then_with(|| Ordering::Equal);
304+
/// assert_eq!(result, Ordering::Less);
305+
///
306+
/// let result = Ordering::Less.then_with(|| Ordering::Greater);
307+
/// assert_eq!(result, Ordering::Less);
308+
///
309+
/// let result = Ordering::Equal.then_with(|| Ordering::Equal);
310+
/// assert_eq!(result, Ordering::Equal);
311+
///
312+
/// let x: (i64, i64, i64) = (1, 2, 7);
313+
/// let y: (i64, i64, i64) = (1, 5, 3);
314+
/// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
315+
///
316+
/// assert_eq!(result, Ordering::Less);
317+
/// ```
318+
#[unstable(feature = "ordering_chaining", issue = "37053")]
319+
pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
320+
match self {
321+
Equal => f(),
322+
_ => self,
323+
}
324+
}
251325
}
252326

253327
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).

src/libcoretest/cmp.rs

+26
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ fn test_ordering_order() {
4141
assert_eq!(Greater.cmp(&Less), Greater);
4242
}
4343

44+
#[test]
45+
fn test_ordering_then() {
46+
assert_eq!(Equal.then(Less), Less);
47+
assert_eq!(Equal.then(Equal), Equal);
48+
assert_eq!(Equal.then(Greater), Greater);
49+
assert_eq!(Less.then(Less), Less);
50+
assert_eq!(Less.then(Equal), Less);
51+
assert_eq!(Less.then(Greater), Less);
52+
assert_eq!(Greater.then(Less), Greater);
53+
assert_eq!(Greater.then(Equal), Greater);
54+
assert_eq!(Greater.then(Greater), Greater);
55+
}
56+
57+
#[test]
58+
fn test_ordering_then_with() {
59+
assert_eq!(Equal.then_with(|| Less), Less);
60+
assert_eq!(Equal.then_with(|| Equal), Equal);
61+
assert_eq!(Equal.then_with(|| Greater), Greater);
62+
assert_eq!(Less.then_with(|| Less), Less);
63+
assert_eq!(Less.then_with(|| Equal), Less);
64+
assert_eq!(Less.then_with(|| Greater), Less);
65+
assert_eq!(Greater.then_with(|| Less), Greater);
66+
assert_eq!(Greater.then_with(|| Equal), Greater);
67+
assert_eq!(Greater.then_with(|| Greater), Greater);
68+
}
69+
4470
#[test]
4571
fn test_user_defined_eq() {
4672
// Our type.

src/libcoretest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(unique)]
3535
#![feature(iter_max_by)]
3636
#![feature(iter_min_by)]
37+
#![feature(ordering_chaining)]
3738
#![feature(result_unwrap_or_default)]
3839

3940
extern crate core;

0 commit comments

Comments
 (0)