Skip to content

Commit

Permalink
Auto merge of #27371 - Gankro:str-clone, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jul 30, 2015
2 parents f97a82f + 3e954a8 commit 87055a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,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]
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
8 changes: 8 additions & 0 deletions src/libcollectionstest/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 87055a6

Please # to comment.