Skip to content

Conversation

crgl
Copy link
Contributor

@crgl crgl commented Oct 29, 2019

This commit allows VecDeque::truncate to take advantage of its (largely) contiguous memory layout and is consistent with the change in #64432 for Vec. As with the change to Vec::truncate, this changes both:

  • the drop order, from back-to-front to front-to-back
  • the behavior when dropping an element panics

For consistency, it also changes the behavior when dropping an element panics for VecDeque::clear.

These changes in behavior can be observed. This example (playground)

use std::collections::VecDeque;

fn main() {
    struct Bomb(usize);
    impl Drop for Bomb {
        fn drop(&mut self) {
            panic!(format!("{}", self.0));
        }
    }
    let mut v = VecDeque::from(vec![Bomb(0), Bomb(1)]);
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        v.truncate(0);
    }));
    std::mem::forget(v);
}

panics printing 1 today and succeeds. v.clear() panics printing 0 today and succeeds. With the change, v.clear(), v.truncate(0), and dropping the VecDeque all panic printing 0 first and then abort with a double-panic printing 1.

The motivation for this was making VecDeque::truncate more efficient since it was used in the implementation of VecDeque::clone_from (#65069), but it also makes behavior more consistent within the VecDeque and with Vec if that change is accepted (this probably doesn't make sense to merge if not).

This might need a crater run and an FCP as well.

@rust-highfive
Copy link
Contributor

r? @Mark-Simulacrum

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 29, 2019
@Mark-Simulacrum
Copy link
Member

Going to move this for to r? @alexcrichton as this is a libs team question.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 30, 2019
@alexcrichton
Copy link
Member

Implementation looks reasonable to me and I think the change also makes sense, thanks @crgl!

Gonna do a query of other libs folks to ensure we're all on board with the semantic changes here, which are in line with the recent semantic changes we made to Vec

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Oct 30, 2019

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 30, 2019
@rfcbot
Copy link

rfcbot commented Oct 31, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 31, 2019
@JohnCSimon
Copy link
Member

Ping from triage - this has sat idle for a week
@crgl @clarfon does this need code changes or is it ready for review + merge?

cc: @alexcrichton

@clarfonthey
Copy link
Contributor

I don't think it needs any changes.

@crgl
Copy link
Contributor Author

crgl commented Nov 9, 2019

I think it should be good to merge then, thanks!

@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Nov 10, 2019
@rfcbot
Copy link

rfcbot commented Nov 10, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Nov 10, 2019
@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Nov 11, 2019

📌 Commit 27e0ab5 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 11, 2019
@bors
Copy link
Collaborator

bors commented Nov 11, 2019

⌛ Testing commit 27e0ab5 with merge bc0e288...

bors added a commit that referenced this pull request Nov 11, 2019
Use ptr::drop_in_place for VecDeque::truncate and VecDeque::clear

This commit allows `VecDeque::truncate` to take advantage of its (largely) contiguous memory layout and is consistent with the change in #64432 for `Vec`. As with the change to `Vec::truncate`, this changes both:

- the drop order, from back-to-front to front-to-back
- the behavior when dropping an element panics

For consistency, it also changes the behavior when dropping an element panics for `VecDeque::clear`.

These changes in behavior can be observed. This example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d0b1f2edc123437a2f704cbe8d93d828))
```rust
use std::collections::VecDeque;

fn main() {
    struct Bomb(usize);
    impl Drop for Bomb {
        fn drop(&mut self) {
            panic!(format!("{}", self.0));
        }
    }
    let mut v = VecDeque::from(vec![Bomb(0), Bomb(1)]);
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        v.truncate(0);
    }));
    std::mem::forget(v);
}
```
panics printing `1` today and succeeds. `v.clear()` panics printing `0` today and succeeds. With the change, `v.clear()`, `v.truncate(0)`, and dropping the `VecDeque` all panic printing `0` first and then abort with a double-panic printing `1`.

The motivation for this was making `VecDeque::truncate` more efficient since it was used in the implementation of `VecDeque::clone_from` (#65069), but it also makes behavior more consistent within the `VecDeque` and with `Vec` if that change is accepted (this probably doesn't make sense to merge if not).

This might need a crater run and an FCP as well.
@bors
Copy link
Collaborator

bors commented Nov 11, 2019

☀️ Test successful - checks-azure
Approved by: alexcrichton
Pushing bc0e288 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 11, 2019
@bors bors merged commit 27e0ab5 into rust-lang:master Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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