Skip to content

Commit 5d55a0f

Browse files
authored
Fix Drop implementation for BackshiftOnDrop
1 parent 6184c1a commit 5d55a0f

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,23 +2316,23 @@ impl<T, A: Allocator> Vec<T, A> {
23162316
impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> {
23172317
fn drop(&mut self) {
23182318
if self.read_index < self.original_len {
2319-
if self.write_index < self.read_index {
2320-
// SAFETY: Trailing unchecked items must be valid since we never touch them.
2321-
unsafe {
2322-
ptr::copy(
2323-
self.v.as_ptr().add(self.read_index),
2324-
self.v.as_mut_ptr().add(self.write_index),
2325-
self.original_len - self.read_index,
2326-
);
2327-
}
2319+
self.write_index = self.write_index.min(self.read_index);
2320+
let remaining = self.original_len - self.read_index;
2321+
// SAFETY: Trailing unchecked items must be valid since we never touch them.
2322+
unsafe {
2323+
ptr::copy(
2324+
self.v.as_ptr().add(self.read_index),
2325+
self.v.as_mut_ptr().add(self.write_index),
2326+
remaining,
2327+
);
23282328
}
23292329
// SAFETY: After filling holes, all items are in contiguous memory.
23302330
unsafe {
2331-
self.v.set_len(self.original_len - (self.read_index - self.write_index));
2331+
self.v.set_len(self.write_index + remaining);
23322332
}
23332333
return;
23342334
}
2335-
// SAFETY: After filling holes, all items are in contiguous memory.
2335+
// SAFETY: no panic happened, so all items are in contiguous memory.
23362336
unsafe {
23372337
self.v.set_len(self.write_index.min(self.read_index));
23382338
}

0 commit comments

Comments
 (0)