Skip to content

Implement Index and IndexMut for Vec #15652

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

Merged
merged 2 commits into from
Jul 17, 2014
Merged

Implement Index and IndexMut for Vec #15652

merged 2 commits into from
Jul 17, 2014

Conversation

nrc
Copy link
Member

@nrc nrc commented Jul 13, 2014

No description provided.

@huonw
Copy link
Member

huonw commented Jul 13, 2014

This should probably be blocked on #15525, or else we'll risk some surprising segfaults and invalid code.

@erickt
Copy link
Contributor

erickt commented Jul 13, 2014

It'd be nice to talk about the plan for what happens with Vec::get and Vec::get_mut. Originally I thought we should deprecate these methods, but WalrusPony on #rust-internals suggested instead that we could change the old methods return Option<&T>. That would let us right fail-free Vec code.

@emberian
Copy link
Member

Yes, get and get_mut should return to their proper Option<&T> status.

@alexcrichton
Copy link
Member

There are many methods on slice that aren't currently available on Vec, awaiting further judgement for ergonomics. I would expect this to deprecate get and get_mut on Vec to assist in transitioning to foo[T] syntax.

The verdict about the ergonomics of slice/Vec types would decide how the get and get_mut methods on slices would be accessed. It would be nice to reduce duplication for now.

@retep998
Copy link
Member

So perhaps deprecate get and get_mut for now, then later remove them, and then even later re-add them with Option<&T> if we decide to duplicate that sort of functionality between Vec and &[T].

@lilyball
Copy link
Contributor

Assuming Vec ends up implementing Deref<[T]> and DerefMut<[T]> post-DST, then I think it's reasonable to remove all Vec methods that it would inherit from [T]. get/get_mut are part of that.

But in the meantime I'm in favor of deprecating get/get_mut along side the implementation of the indexing traits.

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

So I tried deprecating get and get_mut and there are a lot of warnings when building rustc and errors in librustrt (and presumably other crates which deny deprecated). So is it worth deprecating now or waiting and doing and a changeover from get to indexing in rustc?

@huonw
Copy link
Member

huonw commented Jul 14, 2014

Can they be changed in this PR? (Doing it in a separate commit would be nice, if it does work.)

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

Not very easily - it would mean using cfgs for 10s or 100s of methods, even then I'm not sure if you can gate #deprecated on a cfg, so you would still get warnings. Much easier to wait for a snapshot.

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

#15525 has a fix and doing anything for get/get_mut now seems impractical. So, r?

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

Filed #15672 as a follow up to deprecate get and get_mut

@alexcrichton
Copy link
Member

This shouldn't need to wait for a snapshot, everything should be able to move immediately to the new syntax, what were the roadblocks you encountered?

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

I changed the first error I saw in librustrt and that gave me a compile error when doing the stage0 build because indexing for Vecs is not supported. Perhaps the new indexing work in the compiler hasn't made it to the snapshot and that is what we need?

error: cannot index a value of type &mut collections::vec::Vec<core::option::Option<(*const u8,Box<local_data::LocalData:Send>,uint)>>

@huonw
Copy link
Member

huonw commented Jul 14, 2014

Maybe that isn't derefing, and so needs (*v)[i]? (eww)

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

Yeah, on second glance it looks like it might be a problem like that - I tried changing another use of get and it seems to be OK

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

And it turns out sometimes you want a &mut ref to pass somewhere rather than do an immediate assignment, so maybe we can't deprecate get_mut in any case.

@huonw
Copy link
Member

huonw commented Jul 14, 2014

I think that's handled by &mut v[i]?

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

That won't deref v[i] to a value then re-ref it?

@huonw
Copy link
Member

huonw commented Jul 14, 2014

No, it shouldn't. (Otherwise vector indexing would be useless for non-Copy types.)

@lilyball
Copy link
Contributor

&mut v[i] is the expected way to get a &mut reference to an indexed element. You can consider it roughly equivalent to &mut *v.index_mut(&i) if you want, which is perfectly valid (and doesn't cause any issue with non-Copy types).

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

So, that might all be moot - #12825 is a problem here too - the compiler prefers IndexMut to Index which causes problems if the vec is not mutable. So, I think I will just implement Index and leave get_mut for now.

@alexcrichton
Copy link
Member

I believe that no matter what we will want these implementations in any future world basically exactly as-is, so I'm going to r+ this. We can work on the deprecation of get and get_mut in a later PR as it sounds like it's nontrivial.

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

@alexcrichton I believe it might be better for now, not to implement IndexMut - it prevents Index working. In that case deprecating get should not be too hard, having a go atm.

@nrc
Copy link
Member Author

nrc commented Jul 14, 2014

@alexcrichton r? on just Vec::get ?

@sfackler
Copy link
Member

There are some doc test failures: https://travis-ci.org/rust-lang/rust/jobs/29948445

@richo
Copy link
Contributor

richo commented Jul 15, 2014

<3 this makes writing code involving Vec a jillion times less frustrating

@bors bors closed this Jul 17, 2014
@bors bors merged commit aa760a8 into rust-lang:master Jul 17, 2014
@nrc nrc deleted the vec_index branch July 17, 2014 04:00
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants