Skip to content

Add Box::leak<'a>(Box<T>) -> &'a mut T where T: 'a #45881

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

Merged
merged 7 commits into from
Nov 23, 2017
Merged
Changes from all 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
53 changes: 53 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,59 @@ impl<T: ?Sized> Box<T> {
pub fn into_unique(b: Box<T>) -> Unique<T> {
unsafe { mem::transmute(b) }
}

/// Consumes and leaks the `Box`, returning a mutable reference,
/// `&'a mut T`. Here, the lifetime `'a` may be chosen to be `'static`.
///
/// 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. If this is not acceptable, the reference should first be wrapped
/// with the [`Box::from_raw`] function producing a `Box`. This `Box` can
/// then be dropped which will properly destroy `T` and release the
/// allocated memory.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Box::leak(b)` instead of `b.leak()`. This
/// is so that there is no conflict with a method on the inner type.
///
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
///
/// # Examples
///
/// Simple usage:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is a documentation line simply saying "Simple usage" before an example needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used it to differentiate between "simple usage" and "unsized data" - I think it makes it a bit more readable - but I can always remove it.

///
/// ```
/// #![feature(box_leak)]
///
/// fn main() {
/// let x = Box::new(41);
/// let static_ref: &'static mut usize = Box::leak(x);
/// *static_ref += 1;
/// assert_eq!(*static_ref, 42);
/// }
/// ```
///
/// Unsized data:
///
/// ```
/// #![feature(box_leak)]
///
/// fn main() {
/// let x = vec![1, 2, 3].into_boxed_slice();
/// let static_ref = Box::leak(x);
/// static_ref[0] = 4;
/// assert_eq!(*static_ref, [4, 2, 3]);
/// }
/// ```
#[unstable(feature = "box_leak", reason = "needs an FCP to stabilize",
issue = "46179")]
#[inline]
pub fn leak<'a>(b: Box<T>) -> &'a mut T
where
T: 'a // Technically not needed, but kept to be explicit.
{
unsafe { &mut *Box::into_raw(b) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down