Change:
We need to find a way to implement Clone for where . In user code, "cloning" such a box manually is simple: will do it with a short roundtrip through which should be unproblematic. In the rust distribution, we can't impl Clone easily since the tools to do so are in libcollections but the Box type lives in liballoc.
You do not need vec for the implementation. Something like the following would work: Note that because can panic it is required to keep track of the partial result so the correct stuff is dropped.
Yes, that's nice that it can be carefully solved by essentially extracting the needed parts from what Vec does. But the Box code is otherwise so clean, it doesn't feel like the right way to do it.
You're welcome to suggest an alternative but I doubt it can be done in a manner that is both correct and significantly cleaner :)
Your fundamental analysis is great: just isn't an exception safe representation while filling it with elements. It needs to keep track of the fill level, so a representation like Vec or BoxBuild is needed...
Clone is also wanted for , but that can be much simpler: There is no panic issue, and it can use a simple memcpy of the data.

Discussion:
use core::prelude::*; use heap; <ins> use raw_vec::RawVec; </ins> use core::any::Any; use core::cmp::Ordering;

use core::mem; use core::ops::{CoerceUnsized, Deref, DerefMut}; use core::ops::{Placer, Boxed, Place, InPlace, BoxPlace}; <del> use core::ptr::Unique; </del> <ins> use core::ptr::{self, Unique}; </ins> use core::raw::{TraitObject}; /// A value that represents the heap. This is the default place that the `box`

} impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {} <ins> #[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.offset(self.len as isize) }; while data != max { unsafe { ptr::read(data); data = data.offset(1); } } } } } } </ins>

#![feature(core)] #![feature(core_intrinsics)] #![feature(core_prelude)] <ins> #![feature(core_slice_ext)] </ins> #![feature(custom_attribute)] #![feature(fundamental)] #![feature(lang_items)]

assert_eq!(ys, [1, 2, 3]); } <ins> #[test] fn test_box_slice_clone() { let data = vec![vec![0, 1], vec![0], vec![1]]; let data2 = data.clone().into_boxed_slice().clone().to_vec(); assert_eq!(data, data2); } #[test] fn test_box_slice_clone_panics() { use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread::spawn; struct Canary { count: Arc<AtomicUsize>, panics: bool } impl Drop for Canary { fn drop(&mut self) { self.count.fetch_add(1, Ordering::SeqCst); } } impl Clone for Canary { fn clone(&self) -> Self { if self.panics { panic!() } Canary { count: self.count.clone(), panics: self.panics } } } let drop_count = Arc::new(AtomicUsize::new(0)); let canary = Canary { count: drop_count.clone(), panics: false }; let panic = Canary { count: drop_count.clone(), panics: true }; spawn(move || { // When xs is dropped, +5. let xs = vec![canary.clone(), canary.clone(), canary.clone(), panic, canary].into_boxed_slice(); // When panic is cloned, +3. xs.clone(); }).join().unwrap_err(); // Total = 8 assert_eq!(drop_count.load(Ordering::SeqCst), 8); } </ins> mod bench { use std::iter::repeat; use std::{mem, ptr};

use ascii; use borrow::{Cow, ToOwned, Borrow}; use boxed::Box; <del> use clone::Clone; </del> use convert::{Into, From}; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use error::Error;

/// } /// # } /// ``` <del> #[derive(PartialEq, PartialOrd, Eq, Ord, Hash)] </del> <ins> #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)] </ins> #[stable(feature = "rust1", since = "1.0.0")] pub struct CString { inner: Box<[u8]>,

} #[stable(feature = "rust1", since = "1.0.0")] <del> impl Clone for CString { fn clone(&self) -> Self { CString { inner: self.inner.to_owned().into_boxed_slice() } } } #[stable(feature = "rust1", since = "1.0.0")] </del> impl Deref for CString { type Target = CStr;