Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUse Borrow for binary_search and contains methods in the standard library #37761
Conversation
rust-highfive
assigned
brson
Nov 14, 2016
This comment has been minimized.
This comment has been minimized.
|
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. |
bluss
reviewed
Nov 14, 2016
| { | ||
| self.iter().any(|e| e == x) | ||
| self.iter().any(|e| e.borrow() == x) | ||
| } |
This comment has been minimized.
This comment has been minimized.
bluss
Nov 14, 2016
•
Contributor
There's another choice isn't there, to use fn contains<Q: ?Sized>(&self, x: &Q) -> bool where T: PartialEq<Q> instead? I think that is more flexible than using Borrow.
This comment has been minimized.
This comment has been minimized.
christophebiocca
Nov 14, 2016
Author
Contributor
Yes, that seems like it would work better for all the contains implementations, given that anyone who implements Borrow<Q> usually implements PartialEq<Q>. Bit of tunnel vision on my part given the issue description.
The binary searches still do need to use Borrow (unless Ord<RHS> were to become a thing).
This comment has been minimized.
This comment has been minimized.
|
The test failure looks legit unfortunately — so that is a hint that it can cause type inference-related regressions. Should probably get a crater run when it's ready. |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the PR! I've started a crater run to evaluate the impact across the ecosystem here. |
This comment has been minimized.
This comment has been minimized.
|
Unfortunately crater turned up with 28 regressions, so we may not be able to make this change :( |
alexcrichton
added
the
T-libs
label
Nov 15, 2016
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Note other comments, about revising the PR to use PartialEq for contains. I guess with crates having code such as |
This comment has been minimized.
This comment has been minimized.
|
The type-inference regression will still occur, so I suspect this new code will cause essentially the same regressions. |
This comment has been minimized.
This comment has been minimized.
|
PartialEq has a default type parameter though? Edit: just tested this (playpen), not sure why that's not sufficient. |
This comment has been minimized.
This comment has been minimized.
|
There are no regressions due to .binary_search in the crater root regressions. Then, a minority is due to type inference failure or coercion failure when using The absolute majority of the root regressions are due to an unrelated error:
|
This comment has been minimized.
This comment has been minimized.
|
Crater report for current PR reports 10 regressions |
This comment has been minimized.
This comment has been minimized.
|
If we remove the changes to |
This comment has been minimized.
This comment has been minimized.
|
True. The fixes are trivial though and usually lead to code simplification:
|
This comment has been minimized.
This comment has been minimized.
|
@letheed I think we should be mindful/conservative about making this change even if crater said things were all clear. If it's going to break popular crates, then I don't think we should do it, no matter how trivial the fix is. |
This comment has been minimized.
This comment has been minimized.
|
Crater is only an indication of the issues, it's far from all Rust code in existence. |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi And I enjoy that things don't break at every new release of Rust. Still this proposed implementation for |
This comment has been minimized.
This comment has been minimized.
|
Can this be solved by type parameter fallback? If so, it doesn't need Rust 2. |
This comment has been minimized.
This comment has been minimized.
|
@bluss, as best as I can tell type parameter fallback defaults do not work for functions, and it isn't 100% clear to me that they ever could work for functions. |
This comment has been minimized.
This comment has been minimized.
|
@christophebiocca Did you try the feature in #27336? |
This comment has been minimized.
This comment has been minimized.
|
#27336 (comment) seems to indicate that default parameters do not work for |
This comment has been minimized.
This comment has been minimized.
|
Ok, some experiments. It is implemented for functions, because But the example in https://is.gd/cKIj8U says that the default fallback can not be used to cover up for type inference breaks. |
christophebiocca
force-pushed the
christophebiocca:borrow-stdlib-fn-refactor
branch
2 times, most recently
from
db95b4c
to
c470d4a
Nov 27, 2016
This comment has been minimized.
This comment has been minimized.
|
Rebased the branch to only have the I'm not sure what we want to do for the |
This comment has been minimized.
This comment has been minimized.
|
This is kinda unfortunate where in theory we'd want to extend multiple APIs, but backwards compatibility forces us to only extend a few. That has the risk of leading to a surprisingly inconsistent experience across the standard library. Not doing this, however, also leads to a surprise where it's something that "should work" but doesn't. I'm not entirely sure what the best resolution is here. |
This comment has been minimized.
This comment has been minimized.
|
I think I'm in favor. My biggest worry isn't about the inconsistency (which already exists, since we have this functionality for maps but not other structures). Rather, I worry about degrading the docs experience elsewhere. Still, given that we've committed to it for maps, which are one of the most common data structures, we may as well double down (and work on improving docs across the board). |
This comment has been minimized.
This comment has been minimized.
|
@bors: r+ Discussed during triage today and decision was to move forward, thanks for being patient @christophebiocca! |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Dec 20, 2016
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@bors: retry
…On Mon, Dec 19, 2016 at 6:03 PM, bors ***@***.***> wrote:
|
christophebiocca commentedNov 14, 2016
Fixes all standard library methods in #32822 that can be fixed without backwards compatibility issues.