-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
generic slice/container indexing using RangeArgument #31191
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
This is necessary so that crates outside of libcore (e.g. libcollections) are allowed to write both `impl Index<usize> for _` and `impl<R: RangeArgument<usize>> Index<R> for _` without getting overlap errors (essentially, to assume that `usize: !RangeArgument<usize>`).
Also, if there are alternatives to making RangeArgument fundamental that still allow these impls, that would be nice... otherwise we can't stabilize RangeArgument until we're sure that the core range data structures are fully baked. One idea is to simply |
I'll run crater. I don't understand what this is doing but it does look crazy. |
Yay! Basically it replaces a bunch of Index<Range*> with blanket
|
I'm curious about the motivation for this change (aside from cutting down on the number of |
I guess it is mostly to reduce the number of impls and code duplication/potential bug sites. With the addition of inclusive range syntax, the new range structs will also need to impl |
No regressions! |
@aturon I suppose another argument is API consistency. You can drain with any |
@durka Thanks for the explanation, and the PR! I'm not opposed to this change, was just curious where it was coming from. I'm going to cc @rust-lang/libs, and bring this up at the libs team meeting tomorrow where we'll make a final decision. Sorry for the delay, but changes to core APIs like this get extra scrutiny. |
I'm not particularly jazzed about pushing RangeArgument more into the spotlight, since it's kinda a perpetual API-design chaos-vortex that drives great men to madness (weirdly, great women are only driven to Denny's, and the vortex doesn't seem to wield its malice against those who identify otherwise). But in all seriousness this seems basically fine, modulo whether we expect drain/indexing/etc to diverge at all. I don't... think so...? Obviously questions need to be answered about RangeInclusive, and the other managerie like |
Oh I'm not at all worried about the delay. This was more of a "what-if" than a forceful proposal from me. Though I do like the API consistency angle. To be clear, it does break code that was using |
The libs team discussed this during triage today and some thoughts were:
There's unfortunately not really a path to stabilization for |
☔ The latest upstream changes (presumably #31646) made this pull request unmergeable. Please resolve the merge conflicts. |
So, while I was working on #30884 (which is still missing indexing) I came up with the crazy idea to allow indexing things with any
R: RangeArgument<usize>
instead of special-casingRange
,RangeTo
,RangeFrom
andRangeFull
(which already implementRangeArgument
).At first it sounds like it would break all type inference everywhere. But, in my test it doesn't, so it seems like a crater run would be interesting to see if there are type inference regressions that we didn't think of.
It does require moving
RangeArgument
tocore::ops
(so that slices can use it) and marking it#[fundamental]
so that it's possible to write theIndex
impls in libcollections (see the commit comment).TL;DR @brson this is crazy, but crater me maybe?
cc @bluss @eddyb