-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
impl ops::Try for Ordering #76041
Closed
Closed
impl ops::Try for Ordering #76041
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||
// run-pass | ||||||
|
||||||
use std::cmp::Ordering; | ||||||
use std::iter; | ||||||
|
||||||
#[derive(Eq, PartialEq)] | ||||||
struct Test(bool, u8, &'static str); | ||||||
|
||||||
const BOOL_VALUES: [bool; 2] = [true, false]; | ||||||
const U8_VALUES: [u8; 3] = [1, 2, 3]; | ||||||
const STR_VALUES: [&str; 3] = ["a", "ab", "c"]; | ||||||
|
||||||
fn test_values() -> impl Iterator<Item = Test> { | ||||||
BOOL_VALUES.iter().flat_map(|&a| { | ||||||
U8_VALUES.iter().flat_map(move |&b| { | ||||||
STR_VALUES.iter().map(move |&c| Test(a, b, c)) | ||||||
}) | ||||||
}) | ||||||
} | ||||||
|
||||||
// This Ord implementation should behave the same as #[derive(Ord)], | ||||||
// but uses the Try operator on Ordering values | ||||||
impl Ord for Test { | ||||||
fn cmp(&self, other: &Self) -> Ordering { | ||||||
self.0.cmp(&other.0)?; | ||||||
self.1.cmp(&other.1)?; | ||||||
self.2.cmp(&other.2) | ||||||
} | ||||||
} | ||||||
|
||||||
impl PartialOrd for Test { | ||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||||||
Some(self.cmp(other)) | ||||||
} | ||||||
} | ||||||
|
||||||
fn test_struct_cmp() { | ||||||
for test1 in test_values() { | ||||||
let test1_alt = (test1.0, test1.1, test1.2); | ||||||
for test2 in test_values() { | ||||||
let test2_alt = (test2.0, test2.1, test2.2); | ||||||
assert_eq!(test1.cmp(&test2), test1_alt.cmp(&test2_alt)); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// Implement Iterator::cmp() using the Try operator | ||||||
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.
Suggested change
For clarity. |
||||||
fn cmp<A, I1, I2>(mut iter1: I1, mut iter2: I2) -> Ordering | ||||||
where | ||||||
A: Ord, | ||||||
I1: Iterator<Item = A>, | ||||||
I2: Iterator<Item = A>, | ||||||
{ | ||||||
loop { | ||||||
match (iter1.next(), iter2.next()) { | ||||||
(Some(x), Some(y)) => x.cmp(&y)?, | ||||||
(x, y) => return x.cmp(&y), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
fn u8_sequences() -> impl Iterator<Item = Vec<u8>> { | ||||||
iter::once(vec![]) | ||||||
.chain(U8_VALUES.iter().map(|&a| vec![a])) | ||||||
.chain(U8_VALUES.iter().flat_map(|&a| { | ||||||
U8_VALUES.iter().map(move |&b| vec![a, b]) | ||||||
})) | ||||||
.chain(U8_VALUES.iter().flat_map(|&a| { | ||||||
U8_VALUES.iter().flat_map(move |&b| { | ||||||
U8_VALUES.iter().map(move |&c| vec![a, b, c]) | ||||||
}) | ||||||
})) | ||||||
} | ||||||
|
||||||
fn test_slice_cmp() { | ||||||
for sequence1 in u8_sequences() { | ||||||
for sequence2 in u8_sequences() { | ||||||
assert_eq!( | ||||||
cmp(sequence1.iter().copied(), sequence2.iter().copied()), | ||||||
sequence1.iter().copied().cmp(sequence2.iter().copied()), | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
fn main() { | ||||||
test_struct_cmp(); | ||||||
test_slice_cmp(); | ||||||
} |
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.
For clarity