-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Swap the 2 variants of Option<T> #49499
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,23 +146,23 @@ | |
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use iter::{FromIterator, FusedIterator, TrustedLen}; | ||
use {mem, ops}; | ||
use {cmp, intrinsics, mem, ops}; | ||
|
||
// Note that this is not a lang item per se, but it has a hidden dependency on | ||
// `Iterator`, which is one. The compiler assumes that the `next` method of | ||
// `Iterator` is an enumeration with one type parameter and two variants, | ||
// which basically means it must be `Option`. | ||
|
||
/// The `Option` type. See [the module level documentation](index.html) for more. | ||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] | ||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] // PartialOrd and Ord by hand below. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub enum Option<T> { | ||
/// No value | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
None, | ||
/// Some value `T` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
Some(#[stable(feature = "rust1", since = "1.0.0")] T), | ||
/// No value | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
None, | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
|
@@ -981,6 +981,107 @@ impl<T> From<T> for Option<T> { | |
} | ||
} | ||
|
||
// The Option<T> type used to be defined as { None, Some(T) }, but for codegen | ||
// reasons we reversed it in #49499 to reduce the amount of work that needs | ||
// to be done for Result<T, ()> <-> Option<T> conversions. Keeping the derived | ||
// Ord and PartialOrd implementations would make that swap a breaking change. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<T> Ord for Option<T> | ||
where | ||
T: Ord, | ||
{ | ||
#[inline] | ||
fn cmp(&self, other: &Self) -> cmp::Ordering { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
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. Couldn't this use 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. Yeah I guess I could use |
||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this.cmp(other), | ||
_ => cmp::Ordering::Equal, | ||
} | ||
} else { | ||
other_discr.cmp(&self_discr) | ||
} | ||
} | ||
} | ||
|
||
// See comment on the Ord impl above. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<T> PartialOrd for Option<T> | ||
where | ||
T: PartialOrd, | ||
{ | ||
#[inline] | ||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this.partial_cmp(other), | ||
_ => Some(cmp::Ordering::Equal), | ||
} | ||
} else { | ||
other_discr.partial_cmp(&self_discr) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn lt(&self, other: &Self) -> bool { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this < other, | ||
_ => false, | ||
} | ||
} else { | ||
other_discr < self_discr | ||
} | ||
} | ||
|
||
#[inline] | ||
fn le(&self, other: &Self) -> bool { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this <= other, | ||
_ => true, | ||
} | ||
} else { | ||
other_discr <= self_discr | ||
} | ||
} | ||
|
||
#[inline] | ||
fn gt(&self, other: &Self) -> bool { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this > other, | ||
_ => false, | ||
} | ||
} else { | ||
other_discr > self_discr | ||
} | ||
} | ||
|
||
#[inline] | ||
fn ge(&self, other: &Self) -> bool { | ||
let self_discr = unsafe { intrinsics::discriminant_value(self) } as isize; | ||
let other_discr = unsafe { intrinsics::discriminant_value(other) } as isize; | ||
if self_discr == other_discr { | ||
match (self, other) { | ||
(&Some(ref this), &Some(ref other)) => this >= other, | ||
_ => true, | ||
} | ||
} else { | ||
other_discr >= self_discr | ||
} | ||
} | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// The Option Iterators | ||
///////////////////////////////////////////////////////////////////////////// | ||
|
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
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.
Document (in a comment) the reason for this order and the manual impls below
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.
Should it a doc comment, or just a comment in the source?
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.
I added some comments and made them reference each other.