From 04ab4a34719979d7f17b4c0df82ac8dbf08af1ad Mon Sep 17 00:00:00 2001 From: Michael Layzell Date: Sun, 24 May 2015 21:45:29 -0400 Subject: [PATCH 1/2] Mark `boxed::into_raw` as safe By the same logic that `mem::forget` is safe, `boxed::into_raw` is actually a safe function. Fixes #25755. --- src/liballoc/boxed.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 35732dacd44f9..6633e48a814f6 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -139,24 +139,20 @@ impl Box { /// convert pointer back to `Box` with `Box::from_raw` function, because /// `Box` does not specify, how memory is allocated. /// -/// Function is unsafe, because result of this function is no longer -/// automatically managed that may lead to memory or other resource -/// leak. -/// /// # Examples /// ``` /// # #![feature(alloc)] /// use std::boxed; /// /// let seventeen = Box::new(17u32); -/// let raw = unsafe { boxed::into_raw(seventeen) }; +/// let raw = boxed::into_raw(seventeen); /// let boxed_again = unsafe { Box::from_raw(raw) }; /// ``` #[unstable(feature = "alloc", reason = "may be renamed")] #[inline] -pub unsafe fn into_raw(b: Box) -> *mut T { - mem::transmute(b) +pub fn into_raw(b: Box) -> *mut T { + unsafe { mem::transmute(b) } } #[stable(feature = "rust1", since = "1.0.0")] From d416fc1d40e39661153b2183726f7e71f51d24e3 Mon Sep 17 00:00:00 2001 From: Michael Layzell Date: Mon, 25 May 2015 14:44:22 -0400 Subject: [PATCH 2/2] Remove unsafe block around boxed::into_raw() as it is now safe --- src/libstd/sync/mpsc/spsc_queue.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libstd/sync/mpsc/spsc_queue.rs b/src/libstd/sync/mpsc/spsc_queue.rs index b72da91c0a075..a0ed52d4d3c87 100644 --- a/src/libstd/sync/mpsc/spsc_queue.rs +++ b/src/libstd/sync/mpsc/spsc_queue.rs @@ -80,12 +80,10 @@ unsafe impl Sync for Queue { } impl Node { fn new() -> *mut Node { - unsafe { - boxed::into_raw(box Node { - value: None, - next: AtomicPtr::new(ptr::null_mut::>()), - }) - } + boxed::into_raw(box Node { + value: None, + next: AtomicPtr::new(ptr::null_mut::>()), + }) } }