about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlexis Beingessner <a.beingessner@gmail.com>2015-07-27 09:06:00 -0700
committerAlexis Beingessner <a.beingessner@gmail.com>2015-07-29 18:43:01 -0700
commit3e954a8cb2fd094d79713059d83d37b2daa7b396 (patch)
treee09612cbe34258da5081b07862b9c9b25f102de3 /src
parentba324694d6116cb0a9a73e29acb043d251a1e6f5 (diff)
downloadrust-3e954a8cb2fd094d79713059d83d37b2daa7b396.tar.gz
rust-3e954a8cb2fd094d79713059d83d37b2daa7b396.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/liballoc/boxed.rs13
-rw-r--r--src/liballoc/lib.rs1
-rw-r--r--src/libcollectionstest/str.rs8
3 files changed, 22 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index db338eaaf00..58c10b6ea59 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -321,6 +321,19 @@ impl<T: Clone> Clone for Box<T> {
     }
 }
 
+
+#[stable(feature = "box_slice_clone", since = "1.3.0")]
+impl Clone for Box<str> {
+    fn clone(&self) -> Self {
+        let len = self.len();
+        let buf = RawVec::with_capacity(len);
+        unsafe {
+            ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
+            mem::transmute(buf.into_box()) // bytes to str ~magic
+        }
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
     #[inline]
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs
index 03cf4fc9fc1..0e9b01e5c23 100644
--- a/src/liballoc/lib.rs
+++ b/src/liballoc/lib.rs
@@ -92,6 +92,7 @@
 #![feature(unsafe_no_drop_flag, filling_drop)]
 #![feature(unsize)]
 #![feature(core_slice_ext)]
+#![feature(core_str_ext)]
 
 #![cfg_attr(test, feature(test, alloc, rustc_private, box_raw))]
 #![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")),
diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs
index 5419c43ba09..4cccb29b41c 100644
--- a/src/libcollectionstest/str.rs
+++ b/src/libcollectionstest/str.rs
@@ -1766,6 +1766,14 @@ fn test_into_string() {
     assert_eq!(string.clone().into_boxed_slice().into_string(), string);
 }
 
+#[test]
+fn test_box_slice_clone() {
+    let data = String::from("hello HELLO hello HELLO yes YES 5 中ä华!!!");
+    let data2 = data.clone().into_boxed_slice().clone().into_string();
+
+    assert_eq!(data, data2);
+}
+
 mod pattern {
     use std::str::pattern::Pattern;
     use std::str::pattern::{Searcher, ReverseSearcher};