-
Notifications
You must be signed in to change notification settings - Fork 13.4k
(#102929) Implement String:leak
#102941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(#102929) Implement String:leak
#102941
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -69,7 +69,7 @@ use crate::boxed::Box; | |||||
use crate::collections::TryReserveError; | ||||||
use crate::str::{self, Chars, Utf8Error}; | ||||||
#[cfg(not(no_global_oom_handling))] | ||||||
use crate::str::{from_boxed_utf8_unchecked, FromStr}; | ||||||
use crate::str::{from_boxed_utf8_unchecked, from_utf8_unchecked_mut, FromStr}; | ||||||
use crate::vec::Vec; | ||||||
|
||||||
/// A UTF-8–encoded, growable string. | ||||||
|
@@ -1849,6 +1849,34 @@ impl String { | |||||
let slice = self.vec.into_boxed_slice(); | ||||||
unsafe { from_boxed_utf8_unchecked(slice) } | ||||||
} | ||||||
|
||||||
/// Consumes and leaks the `String`, returning a mutable reference to the contents, | ||||||
/// `&'a mut str`. | ||||||
/// | ||||||
/// This function is mainly useful for data that lives for the remainder of | ||||||
/// the program's life. Dropping the returned reference will cause a memory | ||||||
/// leak. | ||||||
/// | ||||||
/// # Examples | ||||||
/// | ||||||
/// Simple usage: | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(string_leak)] | ||||||
/// | ||||||
/// pub fn main() { | ||||||
/// let x = String::from("bucket"); | ||||||
/// let static_ref: &'static mut str = x.leak(); | ||||||
/// assert_eq!(static_ref, "bucket"); | ||||||
/// } | ||||||
finnbear marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// ``` | ||||||
#[cfg(not(no_global_oom_handling))] | ||||||
finnbear marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#[unstable(feature = "string_leak", issue = "102929")] | ||||||
#[inline] | ||||||
pub fn leak<'a>(self) -> &'a mut str { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason to use a generic
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea; as I understand it, variance will still allow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
let slice = self.into_bytes().leak(); | ||||||
unsafe { from_utf8_unchecked_mut(slice) } | ||||||
} | ||||||
} | ||||||
|
||||||
impl FromUtf8Error { | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.