-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Clarify behavior of VecDeque::insert
.
#38581
Conversation
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. |
I think my changes are an improvement, but I'm not super satisfied with them. Please let me know if anyone has ideas for improvement. |
1042e8a
to
1f6b0b5
Compare
/// end is closer to the insertion point will be moved to make room, | ||
/// and all the affected elements will be moved to new positions. | ||
/// Inserts an element within the `VecDeque` at index `index`. All elements previously with | ||
/// indices greater than or equal to `index` will have their indices incremented by one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just reuse the Vec
comment: "Inserts an element at position index within the vector, shifting all elements after it to the right."? I don't know whether that has different meaning for VecDeque
, but seems simpler.
Edit: Link to Vec documentation: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.insert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My hesitation with that comment is that (at least behind the scenes in memory), VecDeque::insert
doesn't necessarily shift all the following items to the back like it does with Vec::insert
. Though, if we're not talking about what happens in-memory it is true as far as indices/positions go, if that makes any sense. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "Inserts an element at position index within the vector, incrementing all indices after index
by one."
That avoids the problem with somewhat implying the underlying memory operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I incorporated it in the latest force push.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though on second thought, your suggestion doesn't really address #37046.
Looks like your PR / commit message title has a typo: |
1f6b0b5
to
c11705e
Compare
VecDequeue::insert
.VecDeque::insert
.
@shepmaster Thanks for pointing that out. It's been fixed now. |
deec978
to
971a019
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, left two minor nits.
/// Inserts an element at `index` within the `VecDeque`. Whichever | ||
/// end is closer to the insertion point will be moved to make room, | ||
/// and all the affected elements will be moved to new positions. | ||
/// Inserts an element at index `index` within the `VecDeque`, shifting all elements with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just "at index
"? "index index
" feels a little odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the latest force push.
/// vec_deque.insert(1, 'd'); | ||
/// | ||
/// let vec = vec_deque.iter().collect::<Vec<_>>(); | ||
/// assert_eq!(vec, [&'a', &'d', &'b', &'c']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this will compile, but perhaps we can omit the &
in front of each char in the array? Perhaps removing those and making it a slice will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That didn't work for me:
error[E0277]: the trait bound `&char: std::cmp::PartialEq<char>` is not satisfied
--> <anon>:12:4
|
12 | assert_eq!(vec, &['a', 'd', 'b', 'c']);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::cmp::PartialEq<char>` is not implemented for `&char`
|
= help: the following implementations were found:
= help: <char as std::cmp::PartialEq>
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[char; 4]>` for `std::vec::Vec<&char>`
= note: this error originates in a macro outside of the current crate
error: aborting due to previous error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see the problem. Can you try changing vec_deque.iter().collect::<Vec<_>>();
to vec_deque.into_iter().collect::<Vec<_>>();
? That should fix the compile error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Fixed in the latest force push.
971a019
to
8f68e23
Compare
8f68e23
to
0ab7812
Compare
Thanks! @bors: r+ rollup |
📌 Commit 0ab7812 has been approved by |
…aumeGomez Clarify behavior of `VecDeque::insert`. Fixes rust-lang#37046.
…aumeGomez Clarify behavior of `VecDeque::insert`. Fixes rust-lang#37046.
⌛ Testing commit 0ab7812 with merge 96d3123... |
💔 Test failed - status-travis |
@bors: retry
* network error
…On Mon, Jan 9, 2017 at 8:06 AM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/190296475>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#38581 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95PkY7pzNqSujpCBOZU50x7r00Jmbks5rQlrxgaJpZM4LVC9j>
.
|
Clarify behavior of `VecDeque::insert`. Fixes #37046.
☀️ Test successful - status-appveyor, status-travis |
Fixes #37046.