-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d41c91c
Add or and or_else for ordering.
rednum def0b4e
Fix trailing whitespace.
rednum ca76d43
Fix comments
rednum 43a0226
Fix doctests
rednum c78a212
Add ordering_chaining feature to libcore/lib.rs
rednum 634715a
Actually fix doctests.
rednum 4e2822c
Rename ordering chaining functions.
rednum 655effe
Merge branch 'master' of https://github.com/rust-lang/rust
rednum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,6 +245,76 @@ impl Ordering { | |
Greater => Less, | ||
} | ||
} | ||
|
||
/// Chain two orderings. | ||
/// | ||
/// 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Chain/Chains/