@@ -269,7 +269,34 @@ 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` then
278+ /// 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+ /// let x = Box::new(5);
292+ /// let ptr = Box::into_unique(x);
293+ /// let x = unsafe { Box::from_unique(ptr) };
294+ /// ```
295+ #[ unstable( feature = "unique" , reason = "needs an RFC to flesh out design" ,
296+ issue = "27730" ) ]
297+ #[ inline]
298+ pub unsafe fn from_unique ( u : Unique < T > ) -> Self {
299+ mem:: transmute ( u)
273300 }
274301
275302 /// Consumes the `Box`, returning the wrapped raw pointer.
@@ -303,13 +330,18 @@ impl<T: ?Sized> Box<T> {
303330 /// After calling this function, the caller is responsible for the
304331 /// memory previously managed by the `Box`. In particular, the
305332 /// 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.
333+ /// proper way to do so is to either convert the `Unique<T>` pointer:
334+ ///
335+ /// - Into a `Box` with the [`Box::from_unique`] function.
336+ ///
337+ /// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`]
338+ /// function.
308339 ///
309340 /// Note: this is an associated function, which means that you have
310341 /// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
311342 /// is so that there is no conflict with a method on the inner type.
312343 ///
344+ /// [`Box::from_unique`]: struct.Box.html#method.from_unique
313345 /// [`Box::from_raw`]: struct.Box.html#method.from_raw
314346 ///
315347 /// # Examples
0 commit comments