-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make #[derive(Copy, Eq, Ord)] imply #[derive(Clone, PartialEq, PartialOrd)] #23905
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
PartialCmpOp, LtOp, LeOp, GtOp, GeOp, | ||
trait_def.expand(cx, mitem, item, push); | ||
|
||
partial_ord::expand_deriving_partial_ord(cx, span, mitem, item, push) |
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.
The derived partial_cmp
can be optimized in this case to just Some(self.cmp(other))
.
EDIT: Hmm, though they're not totally equivalent in the presence of type paramters unfortunately.
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.
Good point. I've got a build running that applies this PartialOrd optimization to non-generic types. I'll push it up when it finishes running.
🎯 |
Out of curiosity, is it still possible to have |
@alexcrichton I think the more common thing would be the opposite since |
Hm true. In general I've been very worried in the past about this form of |
@alexcrichton: with this PR we could no longer provide a manual implementation of |
My main concern is whether this PR requires an RFC. It seems to be at least mildly controversial. Obviously this would have to land by 1.0 though! (I personally acknowledge @alexcrichton's concerns, but think that this PR is a net improvement on the current situation.) |
@sfackler: I just pushed up a patch that simplifies generating |
☔ The latest upstream changes (presumably #23963) made this pull request unmergeable. Please resolve the merge conflicts. |
I rebased on top of HEAD to avoid some merge conflicts and squashed some of the cleanup commits together. |
This allows #[derive(...)]` to create more than one impl
This PR generates a simplified `PartialOrd` implementation when the type is `Ord` and non-generic. It produces essentially: impl ::std::cmp::PartialOrd for $ty { fn partial_cmp(&self, other: &$ty) -> Option<std::cmp::Ordering> { Some(::std::cmp::Ord::cmp(self, other)) } }
Copying this comment to ensure it doesn't get lost: I believe it one should be able to make the simple-impls work via the Also, another minor point: what about supporting Alsoalso, I wonder about the case of optimising #[derive(Ord)]
struct Foo { ... } and struct Foo { ... }
impl Ord for Foo {
fn cmp(&self, other: &Foo) -> Ordering {
// ... lots of stuff ...
}
}
impl PartialOrd for Foo {
fn partial_cmp(&self, other: &Foo) -> Option<Ordering> { Some(self.cmp(other)) }
fn lt(&self, other: &Foo) -> bool { /* supafast */ }
} That said, it seems like this PR is the right default, and we can add an opt-out mechanism in future. |
I thought the PR did permit this, albeit with a FIXME attached? (I tend to think we should just allow it and, at most, lint for it) |
@nikomatsakis: Yes, this PR explicitly handles |
This extracts some of the minor cleanup patches from #23905.
Oh, I wasn't clear: my reading of the PR was that the support for that was only for staging reasons. In any case, a lint (or nothing) sounds good to me. |
This extracts some of the minor cleanup patches from #23905.
This should probably be closed now, as the RFC has been resolved? |
Yes thanks for the reminder @dcrewi! Closing for the same reasons as listed in the RFC. |
This extracts some of the minor cleanup patches from #23905.
This reduces the amount of boilerplate users need to do in order to derive some common traits.
This is related to #23860.
cc @nikomatsakis, @aturon