Skip to content

Commit 3e954a8

Browse files
committed
implement Clone for Box<str>, closes #27323
This is a minor [breaking-change], as it changes what `boxed_str.to_owned()` does (previously it would deref to `&str` and call `to_owned` on that to get a `String`). However `Box<str>` is such an exceptionally rare type that this is not expected to be a serious concern. Also a `Box<str>` can be freely converted to a `String` to obtain the previous behaviour anyway.
1 parent ba32469 commit 3e954a8

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ impl<T: Clone> Clone for Box<T> {
321321
}
322322
}
323323

324+
325+
#[stable(feature = "box_slice_clone", since = "1.3.0")]
326+
impl Clone for Box<str> {
327+
fn clone(&self) -> Self {
328+
let len = self.len();
329+
let buf = RawVec::with_capacity(len);
330+
unsafe {
331+
ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
332+
mem::transmute(buf.into_box()) // bytes to str ~magic
333+
}
334+
}
335+
}
336+
324337
#[stable(feature = "rust1", since = "1.0.0")]
325338
impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
326339
#[inline]

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#![feature(unsafe_no_drop_flag, filling_drop)]
9393
#![feature(unsize)]
9494
#![feature(core_slice_ext)]
95+
#![feature(core_str_ext)]
9596

9697
#![cfg_attr(test, feature(test, alloc, rustc_private, box_raw))]
9798
#![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")),

src/libcollectionstest/str.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,14 @@ fn test_into_string() {
17661766
assert_eq!(string.clone().into_boxed_slice().into_string(), string);
17671767
}
17681768

1769+
#[test]
1770+
fn test_box_slice_clone() {
1771+
let data = String::from("hello HELLO hello HELLO yes YES 5 中ä华!!!");
1772+
let data2 = data.clone().into_boxed_slice().clone().into_string();
1773+
1774+
assert_eq!(data, data2);
1775+
}
1776+
17691777
mod pattern {
17701778
use std::str::pattern::Pattern;
17711779
use std::str::pattern::{Searcher, ReverseSearcher};

0 commit comments

Comments
 (0)