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

Clarify behavior of VecDeque::insert. #38581

Merged
merged 1 commit into from
Jan 9, 2017

Conversation

frewsxcv
Copy link
Member

Fixes #37046.

@rust-highfive
Copy link
Collaborator

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.

@frewsxcv
Copy link
Member Author

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.

/// 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.
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum Dec 23, 2016

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

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

@shepmaster
Copy link
Member

shepmaster commented Dec 23, 2016

Looks like your PR / commit message title has a typo: VecDequeue instead of VecDeque.

@frewsxcv frewsxcv changed the title Clarify behavior of VecDequeue::insert. Clarify behavior of VecDeque::insert. Dec 24, 2016
@frewsxcv
Copy link
Member Author

Looks like your PR / commit message title has a typo: VecDequeue instead of VecDeque.

@shepmaster Thanks for pointing that out. It's been fixed now.

@frewsxcv frewsxcv force-pushed the vecdequeue-insert branch 2 times, most recently from deec978 to 971a019 Compare December 24, 2016 20:15
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a 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
Copy link
Member

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.

Copy link
Member Author

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']);
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member Author

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.

@GuillaumeGomez
Copy link
Member

Thanks!

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Jan 4, 2017

📌 Commit 0ab7812 has been approved by GuillaumeGomez

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jan 4, 2017
…aumeGomez

Clarify behavior of `VecDeque::insert`.

Fixes rust-lang#37046.
steveklabnik added a commit to steveklabnik/rust that referenced this pull request Jan 4, 2017
…aumeGomez

Clarify behavior of `VecDeque::insert`.

Fixes rust-lang#37046.
@bors
Copy link
Contributor

bors commented Jan 9, 2017

⌛ Testing commit 0ab7812 with merge 96d3123...

@bors
Copy link
Contributor

bors commented Jan 9, 2017

💔 Test failed - status-travis

@alexcrichton
Copy link
Member

alexcrichton commented Jan 9, 2017 via email

@bors
Copy link
Contributor

bors commented Jan 9, 2017

⌛ Testing commit 0ab7812 with merge 5c1472a...

bors added a commit that referenced this pull request Jan 9, 2017
Clarify behavior of `VecDeque::insert`.

Fixes #37046.
@bors
Copy link
Contributor

bors commented Jan 9, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: GuillaumeGomez
Pushing 5c1472a to master...

@bors bors merged commit 0ab7812 into rust-lang:master Jan 9, 2017
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.

8 participants