Skip to content

Commit 87055a6

Browse files
committed
Auto merge of #27371 - Gankro:str-clone, r=alexcrichton
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 result anyway.
2 parents f97a82f + 3e954a8 commit 87055a6

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
@@ -325,6 +325,19 @@ impl<T: Clone> Clone for Box<T> {
325325
}
326326
}
327327

328+
329+
#[stable(feature = "box_slice_clone", since = "1.3.0")]
330+
impl Clone for Box<str> {
331+
fn clone(&self) -> Self {
332+
let len = self.len();
333+
let buf = RawVec::with_capacity(len);
334+
unsafe {
335+
ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
336+
mem::transmute(buf.into_box()) // bytes to str ~magic
337+
}
338+
}
339+
}
340+
328341
#[stable(feature = "rust1", since = "1.0.0")]
329342
impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
330343
#[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)