Skip to content

Commit 8597644

Browse files
committed
Auto merge of #67312 - cuviper:clone-box-slice, r=SimonSapin
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]>`.
2 parents 56446fe + 81a6709 commit 8597644

File tree

1 file changed

+1
-42
lines changed

1 file changed

+1
-42
lines changed

src/liballoc/boxed.rs

+1-42
Original file line numberDiff line numberDiff line change
@@ -1046,48 +1046,7 @@ impl<A> FromIterator<A> for Box<[A]> {
10461046
#[stable(feature = "box_slice_clone", since = "1.3.0")]
10471047
impl<T: Clone> Clone for Box<[T]> {
10481048
fn clone(&self) -> Self {
1049-
let mut new = BoxBuilder { data: RawVec::with_capacity(self.len()), len: 0 };
1050-
1051-
let mut target = new.data.ptr();
1052-
1053-
for item in self.iter() {
1054-
unsafe {
1055-
ptr::write(target, item.clone());
1056-
target = target.offset(1);
1057-
};
1058-
1059-
new.len += 1;
1060-
}
1061-
1062-
return unsafe { new.into_box() };
1063-
1064-
// Helper type for responding to panics correctly.
1065-
struct BoxBuilder<T> {
1066-
data: RawVec<T>,
1067-
len: usize,
1068-
}
1069-
1070-
impl<T> BoxBuilder<T> {
1071-
unsafe fn into_box(self) -> Box<[T]> {
1072-
let raw = ptr::read(&self.data);
1073-
mem::forget(self);
1074-
raw.into_box()
1075-
}
1076-
}
1077-
1078-
impl<T> Drop for BoxBuilder<T> {
1079-
fn drop(&mut self) {
1080-
let mut data = self.data.ptr();
1081-
let max = unsafe { data.add(self.len) };
1082-
1083-
while data != max {
1084-
unsafe {
1085-
ptr::read(data);
1086-
data = data.offset(1);
1087-
}
1088-
}
1089-
}
1090-
}
1049+
self.to_vec().into_boxed_slice()
10911050
}
10921051
}
10931052

0 commit comments

Comments
 (0)