diff options
| author | bors <bors@rust-lang.org> | 2019-05-28 11:50:14 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-05-28 11:50:14 +0000 |
| commit | 7da118581c9dc839c8bf3fbb622bab9ce32bbf38 (patch) | |
| tree | 6afa790e230d5ecc9d3ee1849652e00c74688137 /src/liballoc | |
| parent | 837b72c805f98d95a44ad4fc2b43ba6a8acb108a (diff) | |
| parent | 149c53fc442a690ea6a5c309f408cb2160039b9b (diff) | |
| download | rust-7da118581c9dc839c8bf3fbb622bab9ce32bbf38.tar.gz rust-7da118581c9dc839c8bf3fbb622bab9ce32bbf38.zip | |
Auto merge of #61258 - Centril:rollup-l2mof9t, r=Centril
Rollup of 9 pull requests Successful merges: - #61084 (Clarify docs for unreachable! macro) - #61220 (Added error message for E0284) - #61227 (Use .await syntax instead of await!) - #61230 (avoid creating Boxes of uninitalized values in RawVec) - #61237 (Updated the Iterator docs with information about overriding methods.) - #61241 (Check place iterative) - #61242 (Make dest_needs_borrow iterate instead of recurse) - #61247 (Make eval_place iterate instead of recurse) - #61248 (Use Place::local) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 16 | ||||
| -rw-r--r-- | src/liballoc/raw_vec.rs | 10 |
2 files changed, 15 insertions, 11 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index bf8f5b8b91a..76b660fba68 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -395,11 +395,10 @@ 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); + // this makes a copy of the data + let buf: Box<[u8]> = self.as_bytes().into(); unsafe { - ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len); - from_boxed_utf8_unchecked(buf.into_box()) + from_boxed_utf8_unchecked(buf) } } } @@ -546,9 +545,12 @@ impl<T: Copy> From<&[T]> for Box<[T]> { /// println!("{:?}", boxed_slice); /// ``` fn from(slice: &[T]) -> Box<[T]> { - let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() }; - boxed.copy_from_slice(slice); - boxed + let len = slice.len(); + let buf = RawVec::with_capacity(len); + unsafe { + ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); + buf.into_box() + } } } diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index d1fc5ac3b30..0454a564435 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -685,12 +685,14 @@ impl<T, A: Alloc> RawVec<T, A> { impl<T> RawVec<T, Global> { /// Converts the entire buffer into `Box<[T]>`. /// - /// While it is not *strictly* Undefined Behavior to call - /// this procedure while some of the RawVec is uninitialized, - /// it certainly makes it trivial to trigger it. - /// /// Note that this will correctly reconstitute any `cap` changes /// that may have been performed. (see description of type for details) + /// + /// # Undefined Behavior + /// + /// All elements of `RawVec<T, Global>` must be initialized. Notice that + /// the rules around uninitialized boxed values are not finalized yet, + /// but until they are, it is advisable to avoid them. pub unsafe fn into_box(self) -> Box<[T]> { // NOTE: not calling `cap()` here, actually using the real `cap` field! let slice = slice::from_raw_parts_mut(self.ptr(), self.cap); |
