diff options
| author | bors <bors@rust-lang.org> | 2019-10-25 20:41:28 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-10-25 20:41:28 +0000 |
| commit | 246be7e1a557b8ac8287c6842379a0db67770be6 (patch) | |
| tree | 6359f78c7c2bbf20c7e999fa95ddab93ba1a0ea7 /src/liballoc/string.rs | |
| parent | 23f890f10202a71168c6424da0cdf94135d3c40c (diff) | |
| parent | d40c6afba04c906907cb57157c0aec7e12519c94 (diff) | |
| download | rust-246be7e1a557b8ac8287c6842379a0db67770be6.tar.gz rust-246be7e1a557b8ac8287c6842379a0db67770be6.zip | |
Auto merge of #65826 - JohnTitor:rollup-mr6crka, r=JohnTitor
Rollup of 6 pull requests
Successful merges:
- #65705 (Add {String,Vec}::into_raw_parts)
- #65749 (Insurance policy in case `iter.size_hint()` lies.)
- #65799 (Fill tracking issue number for `array_value_iter`)
- #65800 (self-profiling: Update measureme to 0.4.0 and remove non-RAII methods from profiler.)
- #65806 (Add [T]::as_ptr_range() and [T]::as_mut_ptr_range().)
- #65810 (SGX: Clear additional flag on enclave entry)
Failed merges:
r? @ghost
Diffstat (limited to 'src/liballoc/string.rs')
| -rw-r--r-- | src/liballoc/string.rs | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 639124e26cc..d9927c642b2 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -196,20 +196,21 @@ use crate::vec::Vec; /// /// let story = String::from("Once upon a time..."); /// -/// let ptr = story.as_ptr(); +// FIXME Update this when vec_into_raw_parts is stabilized +/// // Prevent automatically dropping the String's data +/// let mut story = mem::ManuallyDrop::new(story); +/// +/// let ptr = story.as_mut_ptr(); /// let len = story.len(); /// let capacity = story.capacity(); /// /// // story has nineteen bytes /// assert_eq!(19, len); /// -/// // Now that we have our parts, we throw the story away. -/// mem::forget(story); -/// /// // We can re-build a String out of ptr, len, and capacity. This is all /// // unsafe because we are responsible for making sure the components are /// // valid: -/// let s = unsafe { String::from_raw_parts(ptr as *mut _, len, capacity) } ; +/// let s = unsafe { String::from_raw_parts(ptr, len, capacity) } ; /// /// assert_eq!(String::from("Once upon a time..."), s); /// ``` @@ -647,6 +648,37 @@ impl String { decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect() } + /// Decomposes a `String` into its raw components. + /// + /// Returns the raw pointer to the underlying data, the length of + /// the string (in bytes), and the allocated capacity of the data + /// (in bytes). These are the same arguments in the same order as + /// the arguments to [`from_raw_parts`]. + /// + /// After calling this function, the caller is responsible for the + /// memory previously managed by the `String`. The only way to do + /// this is to convert the raw pointer, length, and capacity back + /// into a `String` with the [`from_raw_parts`] function, allowing + /// the destructor to perform the cleanup. + /// + /// [`from_raw_parts`]: #method.from_raw_parts + /// + /// # Examples + /// + /// ``` + /// #![feature(vec_into_raw_parts)] + /// let s = String::from("hello"); + /// + /// let (ptr, len, cap) = s.into_raw_parts(); + /// + /// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) }; + /// assert_eq!(rebuilt, "hello"); + /// ``` + #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] + pub fn into_raw_parts(self) -> (*mut u8, usize, usize) { + self.vec.into_raw_parts() + } + /// Creates a new `String` from a length, capacity, and pointer. /// /// # Safety @@ -677,13 +709,16 @@ impl String { /// /// unsafe { /// let s = String::from("hello"); - /// let ptr = s.as_ptr(); + /// + // FIXME Update this when vec_into_raw_parts is stabilized + /// // Prevent automatically dropping the String's data + /// let mut s = mem::ManuallyDrop::new(s); + /// + /// let ptr = s.as_mut_ptr(); /// let len = s.len(); /// let capacity = s.capacity(); /// - /// mem::forget(s); - /// - /// let s = String::from_raw_parts(ptr as *mut _, len, capacity); + /// let s = String::from_raw_parts(ptr, len, capacity); /// /// assert_eq!(String::from("hello"), s); /// } |
