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

generic slice/container indexing using RangeArgument #31191

Closed
wants to merge 6 commits into from

Conversation

durka
Copy link
Contributor

@durka durka commented Jan 25, 2016

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-casing Range, RangeTo, RangeFrom and RangeFull (which already implement RangeArgument).

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 to core::ops (so that slices can use it) and marking it #[fundamental] so that it's possible to write the Index impls in libcollections (see the commit comment).

TL;DR @brson this is crazy, but crater me maybe?

cc @bluss @eddyb

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

durka added 2 commits January 25, 2016 15:54
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>`).
@bluss bluss added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jan 25, 2016
@durka
Copy link
Contributor Author

durka commented Jan 26, 2016

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 impl RangeArgument<usize> for usize and combine even more impls, but then it would need an associated type to specify what indexing returns (scalar vs slice) and that sounds hairy...

@brson
Copy link
Contributor

brson commented Jan 29, 2016

I'll run crater.

I don't understand what this is doing but it does look crazy.

@durka
Copy link
Contributor Author

durka commented Jan 29, 2016

Yay! Basically it replaces a bunch of Index<Range*> with blanket
impls for anything implementing RangeArgument (which is currently
unstable). This was simple (for now) because the current API of
RangeArgument is essentially a copy of Range with the fields changed to
methods.
On Jan 28, 2016 19:30, "Brian Anderson" [email protected] wrote:

I'll run crater.

I don't understand what this is doing but it does look crazy.


Reply to this email directly or view it on GitHub
#31191 (comment).

@aturon
Copy link
Member

aturon commented Jan 29, 2016

@durka

I'm curious about the motivation for this change (aside from cutting down on the number of Index impls needed) -- are there additional impls of RangeArgument you have in mind beyond the ones for range notation?

@durka
Copy link
Contributor Author

durka commented Jan 29, 2016

@aturon

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 RangeArgument (though it needs to be redesigned somewhat for that to be possible...). Besides that... I agree the benefits aren't super clear. It provides another way for libraries to define indexing on slices/collections with just one impl (but I'm not sure what the implications of #[fundamental] are on who's allowed to do that).

@brson
Copy link
Contributor

brson commented Jan 29, 2016

Crater report.

@durka
Copy link
Contributor Author

durka commented Jan 29, 2016

No regressions!

@durka
Copy link
Contributor Author

durka commented Jan 31, 2016

@aturon I suppose another argument is API consistency. You can drain with any R: RangeArgument, so why not index?

@aturon
Copy link
Member

aturon commented Feb 2, 2016

@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.

@aturon aturon added I-needs-decision Issue: In need of a decision. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 2, 2016
@Gankra
Copy link
Contributor

Gankra commented Feb 2, 2016

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 (x,y] that we may well adopt for e.g. BTree.

@durka
Copy link
Contributor Author

durka commented Feb 2, 2016

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 #![feature(collections_range)] -- trivially, by moving RangeArgument, and then in the unlikely case that someone wrote struct S; impl RangeArgument for S { ... } impl Index<S> for [T] { ... }. But to my knowledge (bolstered by crater, though I don't like to take that as proof) code that isn't using the feature doesn't break. And there's the weird #[fundamental] thing, but hopefully the libs team understands the implications of that better than I do.

@alexcrichton
Copy link
Member

The libs team discussed this during triage today and some thoughts were:

  • The change in error messages seems a little unfortunate
  • The addition of #[fundamental] is a blocker for this today

There's unfortunately not really a path to stabilization for #[fundamental], so we're trying to avoid all uses of it if we can. It seems that this does indeed need to infer, however, that usize does not implement RangeArgument. This could in theory be a private trait to libcollections, but that seems a bit unfortunate, I'm not really sure what the best outcome of that is...

@bors
Copy link
Contributor

bors commented Feb 16, 2016

☔ The latest upstream changes (presumably #31646) made this pull request unmergeable. Please resolve the merge conflicts.

@durka durka closed this Feb 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-needs-decision Issue: In need of a decision. S-waiting-on-crater Status: Waiting on a crater run to be completed. 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