Auto merge of #37054 - rednum:master, r=alexcrichton

Add or and or_else for ordering.

Fixes https://github.com/rust-lang/rust/issues/37053 (see discussion in rust-lang/rfcs#1677).
This commit is contained in:
bors 2016-11-02 05:37:33 -07:00 committed by GitHub
commit acfe959701
3 changed files with 101 additions and 0 deletions

View File

@ -248,6 +248,80 @@ impl Ordering {
Greater => Less,
}
}
/// Chains two orderings.
///
/// Returns `self` when it's not `Equal`. Otherwise returns `other`.
/// # Examples
///
/// ```
/// #![feature(ordering_chaining)]
///
/// use std::cmp::Ordering;
///
/// let result = Ordering::Equal.then(Ordering::Less);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.then(Ordering::Equal);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.then(Ordering::Greater);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Equal.then(Ordering::Equal);
/// assert_eq!(result, Ordering::Equal);
///
/// let x: (i64, i64, i64) = (1, 2, 7);
/// let y: (i64, i64, i64) = (1, 5, 3);
/// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
///
/// assert_eq!(result, Ordering::Less);
/// ```
#[unstable(feature = "ordering_chaining", issue = "37053")]
pub fn then(self, other: Ordering) -> Ordering {
match self {
Equal => other,
_ => self,
}
}
/// Chains the ordering with the given function.
///
/// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
/// the result.
///
/// # Examples
///
/// ```
/// #![feature(ordering_chaining)]
///
/// use std::cmp::Ordering;
///
/// let result = Ordering::Equal.then_with(|| Ordering::Less);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.then_with(|| Ordering::Equal);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.then_with(|| Ordering::Greater);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Equal.then_with(|| Ordering::Equal);
/// assert_eq!(result, Ordering::Equal);
///
/// let x: (i64, i64, i64) = (1, 2, 7);
/// let y: (i64, i64, i64) = (1, 5, 3);
/// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
///
/// assert_eq!(result, Ordering::Less);
/// ```
#[unstable(feature = "ordering_chaining", issue = "37053")]
pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
match self {
Equal => f(),
_ => self,
}
}
}
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).

View File

@ -41,6 +41,32 @@ fn test_ordering_order() {
assert_eq!(Greater.cmp(&Less), Greater);
}
#[test]
fn test_ordering_then() {
assert_eq!(Equal.then(Less), Less);
assert_eq!(Equal.then(Equal), Equal);
assert_eq!(Equal.then(Greater), Greater);
assert_eq!(Less.then(Less), Less);
assert_eq!(Less.then(Equal), Less);
assert_eq!(Less.then(Greater), Less);
assert_eq!(Greater.then(Less), Greater);
assert_eq!(Greater.then(Equal), Greater);
assert_eq!(Greater.then(Greater), Greater);
}
#[test]
fn test_ordering_then_with() {
assert_eq!(Equal.then_with(|| Less), Less);
assert_eq!(Equal.then_with(|| Equal), Equal);
assert_eq!(Equal.then_with(|| Greater), Greater);
assert_eq!(Less.then_with(|| Less), Less);
assert_eq!(Less.then_with(|| Equal), Less);
assert_eq!(Less.then_with(|| Greater), Less);
assert_eq!(Greater.then_with(|| Less), Greater);
assert_eq!(Greater.then_with(|| Equal), Greater);
assert_eq!(Greater.then_with(|| Greater), Greater);
}
#[test]
fn test_user_defined_eq() {
// Our type.

View File

@ -34,6 +34,7 @@
#![feature(unique)]
#![feature(iter_max_by)]
#![feature(iter_min_by)]
#![feature(ordering_chaining)]
#![feature(result_unwrap_or_default)]
extern crate core;