Skip to content

Commit

Permalink
Auto merge of #37054 - rednum:master, r=alexcrichton
Browse files Browse the repository at this point in the history
Add or and or_else for ordering.

Fixes #37053 (see discussion in rust-lang/rfcs#1677).
  • Loading branch information
bors committed Nov 2, 2016
2 parents 35a1fef + 655effe commit acfe959
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 26 additions & 0 deletions src/libcoretest/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/libcoretest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit acfe959

Please sign in to comment.