about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2019-12-14 22:28:52 -0800
committerJosh Stone <jistone@redhat.com>2019-12-23 07:35:39 -0700
commit81a6709cf9f6e83b6dd2311f4dcd9e1ef1fba284 (patch)
tree760e80241f2748090fac95e5dd957252b39d72a4 /src/liballoc
parenta916ac22b9f7f1f0f7aba0a41a789b3ecd765018 (diff)
downloadrust-81a6709cf9f6e83b6dd2311f4dcd9e1ef1fba284.tar.gz
rust-81a6709cf9f6e83b6dd2311f4dcd9e1ef1fba284.zip
Simplify Clone for Box<[T]>
The bespoke `BoxBuilder` was basically a very simple `Vec`. Instead,
let's clone to a real `Vec`, with all of its specialization for the
task, then convert back to `Box<[T]>`.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/boxed.rs43
1 files changed, 1 insertions, 42 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index cc01de08caf..87a4924f9be 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -1046,48 +1046,7 @@ impl<A> FromIterator<A> for Box<[A]> {
 #[stable(feature = "box_slice_clone", since = "1.3.0")]
 impl<T: Clone> Clone for Box<[T]> {
     fn clone(&self) -> Self {
-        let mut new = BoxBuilder { data: RawVec::with_capacity(self.len()), len: 0 };
-
-        let mut target = new.data.ptr();
-
-        for item in self.iter() {
-            unsafe {
-                ptr::write(target, item.clone());
-                target = target.offset(1);
-            };
-
-            new.len += 1;
-        }
-
-        return unsafe { new.into_box() };
-
-        // Helper type for responding to panics correctly.
-        struct BoxBuilder<T> {
-            data: RawVec<T>,
-            len: usize,
-        }
-
-        impl<T> BoxBuilder<T> {
-            unsafe fn into_box(self) -> Box<[T]> {
-                let raw = ptr::read(&self.data);
-                mem::forget(self);
-                raw.into_box()
-            }
-        }
-
-        impl<T> Drop for BoxBuilder<T> {
-            fn drop(&mut self) {
-                let mut data = self.data.ptr();
-                let max = unsafe { data.add(self.len) };
-
-                while data != max {
-                    unsafe {
-                        ptr::read(data);
-                        data = data.offset(1);
-                    }
-                }
-            }
-        }
+        self.to_vec().into_boxed_slice()
     }
 }