Skip to content

(#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

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
/// }
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "string_leak", issue = "102929")]
#[inline]
pub fn leak<'a>(self) -> &'a mut str {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to use a generic <'a> instead of just 'static here? I believe the only reason we use <'a> for Vec<T> is for situations where T: !'static.

Suggested change
pub fn leak<'a>(self) -> &'a mut str {
pub fn leak(self) -> &'static mut str {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; as I understand it, variance will still allow the 'static to be used for any 'a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think there is another reason. Vec is generic over A: Allocator and this may apply to String in the future. At that point, the generic lifetime would be bounded by that of the allocator. I don't know if adding the generic lifetime later would be a breaking change. Thoughts?

image
image

let slice = self.into_bytes().leak();
unsafe { from_utf8_unchecked_mut(slice) }
}
}

impl FromUtf8Error {
Expand Down