Skip to content
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 implementation of Ord for Cell<T> and RefCell<T> where T: Ord #33306

Merged
merged 2 commits into from
May 12, 2016

Conversation

vadixidav
Copy link
Contributor

Raised this in issue #33305.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (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.

@vadixidav vadixidav changed the title fix implementation of Ord for Cell<T> and RefCell<T> where T: Ord add implementation of Ord for Cell<T> and RefCell<T> where T: Ord May 1, 2016
@jonas-schievink
Copy link
Contributor

I think these sort of proxy impls for RefCell were omitted intentionally, since they need to borrow the cell, which can cause a panic.

@vadixidav
Copy link
Contributor Author

vadixidav commented May 1, 2016

It is safe code. Borrow on RefCell already can cause a safe panic. Also, the only other way to get Ord on a RefCell is by making a newtype, which was problematic in some code I was writing where I would have to make newtypes and write tons of boilerplate for about 10 types. Also, it would cause a panic anyways if std was passed some sort of closure or used the Ref from the borrow() function when a RefMut was in existence. There is no way to implement Ord for RefCell in any other way. Also, Cell didn't have any Ord either, despite it not having the borrow issue.

@jonas-schievink
Copy link
Contributor

Of course it's safe, but it might be surprising. I don't think there's any other operator impl in the standard library that can panic.

I would have to make newtypes and write tons of boilerplate for about 10 types.

Why isn't struct OrdRefCell<T> enough? Or did you mean you're only using it for 10 different types?

Also, it would cause a panic anyways if std was passed some sort of closure or used the Ref from the borrow() function

The difference is that the .borrow() would have been written explicitly by the user, while this puts it in the standard library (and not even into a regular method, but into otherwise harmless operators like <).

I don't think it's too much of a hassle to have to write x.borrow() < y.borrow(), but then again I don't know your code so YMMV.

Regarding Cell, maybe that's just an oversight. I don't know.

@vadixidav
Copy link
Contributor Author

Eq is implemented on RefCell. That will cause the same issue. To be consistent we should add this as well.

@jonas-schievink
Copy link
Contributor

Huh, indeed. In that case, I agree that these should be added for consistency. Same for Cell.

@sfackler
Copy link
Member

sfackler commented May 1, 2016

For historical context: #25744

@sfackler sfackler added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 1, 2016
@alexcrichton
Copy link
Member

I'd be fine merging this. This is just extending what we already have with PartialEq and the conclusion of #25744 was that the ergonomic win here is worth it.

@alexcrichton
Copy link
Member

The libs team discussed this during triage the other day and the conclusion was to merge. Thanks again for the PR @vadixidav!

@bors: r+ 4dcb637

@bors
Copy link
Contributor

bors commented May 12, 2016

⌛ Testing commit 4dcb637 with merge 992bb13...

bors added a commit that referenced this pull request May 12, 2016
add implementation of Ord for Cell<T> and RefCell<T> where T: Ord

Raised this in issue #33305.
@bors bors merged commit 4dcb637 into rust-lang:master May 12, 2016
@bluss bluss added the relnotes Marks issues that should be documented in the release notes of the next release. label May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants