-
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
Add PartialOrd, Ord derivations to TypeId #38981
Conversation
I want to be able to sort a `Vec` of types which contain `TypeId`s, so an `Ord` derivation would be very useful to me. `Hash` already exists, so the missing `PartialOrd` and `Ord` derivations feel like an oversight to me.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I think that the main reason why Why would you like to sort data in your |
@clarcharr since the entire purpose of Rust's For sorting the names of |
@sdleffler it seems to make more sense in that case to use a Technically you could use radix sort and a vector to do your O(n) deduplication but I suspect that it'll still be slower than a I suspect that if you're in a case where you need to use |
@clarcharr the "dynamic type checking" overhead isn't actually present in my use case; in fact, I could probably just write a custom Also, the dynamic type checking overhead can't be high in any case, can it? I feel like in most cases it'll be so simple to be almost negligible. Equality between |
@rfcbot fcp merge This seems reasonable to me! |
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot concern forward-compatibility Hm I'm a little concerned here about the motivation. To me there is no inherent ordering amongst type ids, and often people might expect an ordering (e.g. i8 < i32) but we would provide no guarantees about this. This ordering would also likely change from release-to-release as the guts of the compiler implementation changes. @sdleffler could you elaborate on why you want to order type ids? |
@alexcrichton my original use case is no longer relevant (since it wasn't actually necessary to distinguish between the values I had by the values of the |
Yeah that makes sense, I've often wished for a way to easily throw something into The risk for reliance here and actually causing breakage I will agree is indeed quite low. If others try to build this out of tree I agree it's better that we do it ourselves in tree, perhaps just with a clause that the exact order can't be relied upon. |
@BurntSushi @aturon @brson ping @alexcrichton I feel pretty okay telling people who expect u8's type ID to be smaller than u32's that they are wrong :P |
@sfackler yes that also seems plausible to me. @sdleffler can you update the docs here to explicitly say that although there's an ordering between two type ids it's subject to change from release-to-release and cannot be relied upon to stay the same? |
@alexcrichton I've added a warning - how's the wording look to you? |
@sdleffer it's already assumed that |
@sdleffler looks good to me, thanks! |
@clarcharr I didn't see that when I glanced over the docs, so I figured I'd add it. |
The motivation that sometimes you just want a ordering for unordered types I find somewhat unconvincing because this is a special case of a general problem. Consider would we add Ord to float? No. What about other types that one might want to establish some ordering for? Is this the only case of a type in std without an ordering that can be applied an artifical ordering? By accepting this it seems like we are setting precedent - are we going to end up with yet another stream of PRs to fill in the gaps of a new policy for other types? |
@brson I think this is an OK precedent to set, personally. For example, allowing file descriptors to be used as an ordered key seems useful for exactly the same reasons, even though the ordering between them isn't semantically significant. |
@bors: r+ |
📌 Commit b08ab1e has been approved by |
Add PartialOrd, Ord derivations to TypeId I want to be able to sort a `Vec` of types which contain `TypeId`s, so an `Ord` derivation would be very useful to me. `Hash` and `PartialEq`/`Eq` already exist, so the missing `PartialOrd` and `Ord` derivations feel like an oversight to me.
Add PartialOrd, Ord derivations to TypeId I want to be able to sort a `Vec` of types which contain `TypeId`s, so an `Ord` derivation would be very useful to me. `Hash` and `PartialEq`/`Eq` already exist, so the missing `PartialOrd` and `Ord` derivations feel like an oversight to me.
☀️ Test successful - status-appveyor, status-travis |
I want to be able to sort a
Vec
of types which containTypeId
s, so anOrd
derivation would be very useful to me.Hash
andPartialEq
/Eq
already exist, so the missingPartialOrd
andOrd
derivations feel like an oversight to me.