about summary refs log tree commit diff
path: root/src/liballoc/collections/vec_deque.rs
diff options
context:
space:
mode:
authorTrevor Spiteri <tspiteri@ieee.org>2020-04-04 14:24:26 +0200
committerTrevor Spiteri <tspiteri@ieee.org>2020-04-04 14:30:33 +0200
commit2b718e8d9ca6efdccf4d72647cb3d3101e15b7fa (patch)
tree35051115f6fd6fb728e25c6619456a03fed85e83 /src/liballoc/collections/vec_deque.rs
parent1b521f57735663de9373679cf8c6502622036bf1 (diff)
downloadrust-2b718e8d9ca6efdccf4d72647cb3d3101e15b7fa.tar.gz
rust-2b718e8d9ca6efdccf4d72647cb3d3101e15b7fa.zip
use ManuallyDrop instead of forget inside collections
This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
Diffstat (limited to 'src/liballoc/collections/vec_deque.rs')
-rw-r--r--src/liballoc/collections/vec_deque.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs
index 94532521a90..c17a37c7bae 100644
--- a/src/liballoc/collections/vec_deque.rs
+++ b/src/liballoc/collections/vec_deque.rs
@@ -12,7 +12,7 @@ use core::cmp::{self, Ordering};
 use core::fmt;
 use core::hash::{Hash, Hasher};
 use core::iter::{once, repeat_with, FromIterator, FusedIterator};
-use core::mem::{self, replace};
+use core::mem::{self, replace, ManuallyDrop};
 use core::ops::Bound::{Excluded, Included, Unbounded};
 use core::ops::{Index, IndexMut, RangeBounds, Try};
 use core::ptr::{self, NonNull};
@@ -2898,12 +2898,12 @@ impl<T> From<Vec<T>> for VecDeque<T> {
     /// This avoids reallocating where possible, but the conditions for that are
     /// strict, and subject to change, and so shouldn't be relied upon unless the
     /// `Vec<T>` came from `From<VecDeque<T>>` and hasn't been reallocated.
-    fn from(mut other: Vec<T>) -> Self {
+    fn from(other: Vec<T>) -> Self {
         unsafe {
+            let mut other = ManuallyDrop::new(other);
             let other_buf = other.as_mut_ptr();
             let mut buf = RawVec::from_raw_parts(other_buf, other.capacity());
             let len = other.len();
-            mem::forget(other);
 
             // We need to extend the buf if it's not a power of two, too small
             // or doesn't have at least one free space
@@ -2955,6 +2955,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
         other.make_contiguous();
 
         unsafe {
+            let other = ManuallyDrop::new(other);
             let buf = other.buf.ptr();
             let len = other.len();
             let cap = other.cap();
@@ -2962,9 +2963,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
             if other.head != 0 {
                 ptr::copy(buf.add(other.tail), buf, len);
             }
-            let out = Vec::from_raw_parts(buf, len, cap);
-            mem::forget(other);
-            out
+            Vec::from_raw_parts(buf, len, cap)
         }
     }
 }