Skip to content

Add then and then_with for ordering. #37054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,76 @@ impl Ordering {
Greater => Less,
}
}

/// Chain two orderings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Chain/Chains/

///
/// Returns `self` when it's not `Equal`. Otherwise returns `other`.
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// let result = Ordering::Equal.or(Ordering::Less);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.or(Ordering::Equal);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.or(Ordering::Greater);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Equal.or(Ordering::Equal);
/// assert_eq!(result, Ordering::Equal);
///
/// let x = (1, 2, 7);
/// let y = (1, 5, 3);
/// let result = x.0.cmp(y.0).or(x.1.cmp(y.1)).or(x.2.cmp(y.2));
///
/// assert_eq!(result, Ordering::Less);
/// ```
#[unstable(feature = "ordering_chaining", issue = "37053")]
pub fn or(self, other: Ordering) -> Ordering {
match self {
Equal => other,
_ => self,
}
}

/// Chain the ordering with given function.
Copy link
Contributor

@apasel422 apasel422 Oct 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chains the ordering with the given function.

///
/// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
/// the result.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// let result = Ordering::Equal.or_else(|| Ordering::Less);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.or_else(|| Ordering::Equal);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Less.or_else(|| Ordering::Greater);
/// assert_eq!(result, Ordering::Less);
///
/// let result = Ordering::Equal.or_else(|| Ordering::Equal);
/// assert_eq!(result, Ordering::Equal);
///
/// let x = (1, 2, 7);
/// let y = (1, 5, 3);
/// let result = x.0.cmp(&y.0).or_else(|| x.1.cmp(&y.1)).or_else(|| x.2.cmp(&y.2));
///
/// assert_eq!(result, Ordering::Less);
/// ```
#[unstable(feature = "ordering_chaining", issue = "37053")]
pub fn or_else<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_or() {
assert_eq!(Equal.or(Less), Less);
assert_eq!(Equal.or(Equal), Equal);
assert_eq!(Equal.or(Greater), Greater);
assert_eq!(Less.or(Less), Less);
assert_eq!(Less.or(Equal), Less);
assert_eq!(Less.or(Greater), Less);
assert_eq!(Greater.or(Less), Greater);
assert_eq!(Greater.or(Equal), Greater);
assert_eq!(Greater.or(Greater), Greater);
}

#[test]
fn test_ordering_or_else() {
assert_eq!(Equal.or_else(|| Less), Less);
assert_eq!(Equal.or_else(|| Equal), Equal);
assert_eq!(Equal.or_else(|| Greater), Greater);
assert_eq!(Less.or_else(|| Less), Less);
assert_eq!(Less.or_else(|| Equal), Less);
assert_eq!(Less.or_else(|| Greater), Less);
assert_eq!(Greater.or_else(|| Less), Greater);
assert_eq!(Greater.or_else(|| Equal), Greater);
assert_eq!(Greater.or_else(|| 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)]

extern crate core;
extern crate test;
Expand Down