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 upSliceExt: More flexible binary_search_by and binary_search_by_key #34683
Comments
pnkfelix
added
the
T-libs
label
Jul 6, 2016
This comment has been minimized.
This comment has been minimized.
|
cc #33018 |
pnkfelix
referenced this issue
Jul 6, 2016
Closed
Tracking issue for slice::binary_search_by_key #33018
sfackler
added
the
I-needs-decision
label
Jul 6, 2016
This comment has been minimized.
This comment has been minimized.
|
This change seems reasonable to me - cc @rust-lang/libs |
brson
added
E-help-wanted
E-easy
and removed
E-help-wanted
labels
Jul 6, 2016
This comment has been minimized.
This comment has been minimized.
|
This is an easy change to make - just make the change as suggested and add the original example as a test case. |
This comment has been minimized.
This comment has been minimized.
|
Yeah we're definitely well within our rights here to assume that no one else is implementing this trait, so in that sense we're fine. This also looks to me like it should be a backwards-compatible modification in terms of not actually causing any code to stop compiling. In that sense I'm all for it as well! I'd like to run crater just to be extra sure of course, but I'd be willing to r+. In terms of scheduling, this won't make 1.10 to be released tomorrow, and it'll definitely get in 1.12 if we land it in the next 6 weeks. We could backport to 1.11 if we really wanted, but it may not be worth it. |
This comment has been minimized.
This comment has been minimized.
|
I'd like to work on this as my first contribution to rust std lib. |
This comment has been minimized.
This comment has been minimized.
|
@creativcoder You got it! |
pnkfelix commentedJul 6, 2016
Someone in
#rust-beginnersasked why the following code (or something similar) does not compile (playpen):The answer, AFAICT, is that the signature for the
fn binary_search_byandfn binary_search_by_keymethods inSliceExtare a bit more restrictive to their callers than warranted.Here is their current signature:
Some things to note:
The methods are stable, but the trait (and its impl on
[T]) are not. This may mean we have room to breathe here.The reason the code is not compiling, as I understand it, is that the bounds on the
Ftype parameter have their lifetimes elided and can be expanded effectively to this:The bound above on
Fis actually quite a strict restriction on the closureF. It is saying that the closure cannot assume that the given reference to an item will live any longer than the invocation of that closure.The reality about how the binary search procedure works means that in practice one can assume something stronger: one can assume that the references passed to the callback actually live as long as the
selfreference of the overall binary search invocation.In other words, I am suggesting that the signature could (and perhaps should) look like this:
In this revised signature, the lifetime of each
Self::Itemreference is quite long-lived.This is enough to allow the original code posted to
#rust-beginnersto compile.I have a demonstration of the above in the playpen here: https://is.gd/ocGRgw
I am pretty sure that if we can make the above change to the signature of
fn binary_search_by_key, we probably should.fn binary_search_byalone, since I am not convinced there is much benefit to generalizing that, though it should, theoretically speaking, be possible to make a test case that would only start compile with an analogous generalization.binary_search_by.)The only question I have at this point is if we can make the change e.g. in 1.11 or 1.12, if 1.10 is released as is.
(I suspect we can make the change. If I understand the stability rules correctly, no one is allowed to implement or even extend the
SliceExttrait outside oflibstd, so in changing the signature of a method like this is only going to affect the code making calls to these methods, and I think that the change suggested here will only make code start compiling; it shouldn't cause any code to stop compiling. Unless I am overlooking some subtle time inference issue or some other problem...)Cc #32110