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

Use ptr::drop_in_place for VecDeque::truncate and VecDeque::clear #65933

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ impl<T> VecDeque<T> {
}
}

/// Shortens the `VecDeque`, dropping excess elements from the back.
/// Shortens the `VecDeque`, keeping the first `len` elements and dropping
/// the rest.
///
/// If `len` is greater than the `VecDeque`'s current length, this has no
/// effect.
Expand All @@ -855,8 +856,31 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "deque_extras", since = "1.16.0")]
pub fn truncate(&mut self, len: usize) {
for _ in len..self.len() {
self.pop_back();
// Safe because:
//
// * Any slice passed to `drop_in_place` is valid; the second case has
// `len <= front.len()` and returning on `len > self.len()` ensures
// `begin <= back.len()` in the first case
// * The head of the VecDeque is moved before calling `drop_in_place`,
// so no value is dropped twice if `drop_in_place` panics
unsafe {
if len > self.len() {
return;
}
crgl marked this conversation as resolved.
Show resolved Hide resolved
let num_dropped = self.len() - len;
let (front, back) = self.as_mut_slices();
if len > front.len() {
let begin = len - front.len();
let drop_back = back.get_unchecked_mut(begin..) as *mut _;
self.head = self.wrap_sub(self.head, num_dropped);
ptr::drop_in_place(drop_back);
} else {
let drop_back = back as *mut _;
let drop_front = front.get_unchecked_mut(len..) as *mut _;
self.head = self.wrap_sub(self.head, num_dropped);
ptr::drop_in_place(drop_front);
ptr::drop_in_place(drop_back);
}
}
}

Expand Down Expand Up @@ -1117,7 +1141,7 @@ impl<T> VecDeque<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn clear(&mut self) {
self.drain(..);
self.truncate(0);
}

/// Returns `true` if the `VecDeque` contains an element equal to the
Expand Down
35 changes: 35 additions & 0 deletions src/liballoc/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,41 @@ fn test_clone_from() {
}
}

#[test]
fn test_vec_deque_truncate_drop() {
static mut DROPS: u32 = 0;
#[derive(Clone)]
struct Elem(i32);
impl Drop for Elem {
fn drop(&mut self) {
unsafe {
DROPS += 1;
}
}
}

let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
for push_front in 0..=v.len() {
let v = v.clone();
let mut tester = VecDeque::with_capacity(5);
for (index, elem) in v.into_iter().enumerate() {
if index < push_front {
tester.push_front(elem);
} else {
tester.push_back(elem);
}
}
assert_eq!(unsafe { DROPS }, 0);
tester.truncate(3);
assert_eq!(unsafe { DROPS }, 2);
tester.truncate(0);
assert_eq!(unsafe { DROPS }, 5);
unsafe {
DROPS = 0;
}
}
}

#[test]
fn issue_53529() {
use crate::boxed::Box;
Expand Down