Skip to content

Commit 0217315

Browse files
committed
Auto merge of #44877 - nvzqz:box-conversions, r=alexcrichton
Improve raw Box conversions This PR has two goals: - Reduce use of `mem::transmute` in `Box` conversions I understand that `mem::transmute`-ing non `#[repr(C)]` types is implementation-defined behavior. This may not matter within the reference implementation of Rust, but I believe it's important to remain consistent. For example, I noticed that `str::from_utf8_unchecked` went from using `mem::transmute` to using pointer casts. - Make `Box` pointer conversions more straightforward regarding `Unique`
2 parents 5f578df + 22298b8 commit 0217315

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

Diff for: src/liballoc/boxed.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,38 @@ impl<T: ?Sized> Box<T> {
269269
#[stable(feature = "box_raw", since = "1.4.0")]
270270
#[inline]
271271
pub unsafe fn from_raw(raw: *mut T) -> Self {
272-
mem::transmute(raw)
272+
Box::from_unique(Unique::new_unchecked(raw))
273+
}
274+
275+
/// Constructs a `Box` from a `Unique<T>` pointer.
276+
///
277+
/// After calling this function, the memory is owned by a `Box` and `T` can
278+
/// then be destroyed and released upon drop.
279+
///
280+
/// # Safety
281+
///
282+
/// A `Unique<T>` can be safely created via [`Unique::new`] and thus doesn't
283+
/// necessarily own the data pointed to nor is the data guaranteed to live
284+
/// as long as the pointer.
285+
///
286+
/// [`Unique::new`]: ../../core/ptr/struct.Unique.html#method.new
287+
///
288+
/// # Examples
289+
///
290+
/// ```
291+
/// #![feature(unique)]
292+
///
293+
/// fn main() {
294+
/// let x = Box::new(5);
295+
/// let ptr = Box::into_unique(x);
296+
/// let x = unsafe { Box::from_unique(ptr) };
297+
/// }
298+
/// ```
299+
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
300+
issue = "27730")]
301+
#[inline]
302+
pub unsafe fn from_unique(u: Unique<T>) -> Self {
303+
mem::transmute(u)
273304
}
274305

275306
/// Consumes the `Box`, returning the wrapped raw pointer.
@@ -295,21 +326,26 @@ impl<T: ?Sized> Box<T> {
295326
#[stable(feature = "box_raw", since = "1.4.0")]
296327
#[inline]
297328
pub fn into_raw(b: Box<T>) -> *mut T {
298-
unsafe { mem::transmute(b) }
329+
Box::into_unique(b).as_ptr()
299330
}
300331

301332
/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
302333
///
303334
/// After calling this function, the caller is responsible for the
304335
/// memory previously managed by the `Box`. In particular, the
305336
/// caller should properly destroy `T` and release the memory. The
306-
/// proper way to do so is to convert the raw pointer back into a
307-
/// `Box` with the [`Box::from_raw`] function.
337+
/// proper way to do so is to either convert the `Unique<T>` pointer:
338+
///
339+
/// - Into a `Box` with the [`Box::from_unique`] function.
340+
///
341+
/// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`]
342+
/// function.
308343
///
309344
/// Note: this is an associated function, which means that you have
310345
/// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
311346
/// is so that there is no conflict with a method on the inner type.
312347
///
348+
/// [`Box::from_unique`]: struct.Box.html#method.from_unique
313349
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
314350
///
315351
/// # Examples

0 commit comments

Comments
 (0)