Skip to content

Commit d1a42ea

Browse files
committed
Add discoverable function for converting Box<T> -> Pin<Box<T>>
1 parent 5e3a560 commit d1a42ea

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/liballoc/boxed.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,19 @@ impl<T: ?Sized> Box<T> {
257257
{
258258
unsafe { &mut *Box::into_raw(b) }
259259
}
260+
261+
/// Converts a `Box<T>` into a `Pin<Box<T>>`
262+
///
263+
/// This conversion does not allocate on the heap and happens in place.
264+
///
265+
/// This is also available via [`From`].
266+
#[unstable(feature = "box_into_pin", issue = "0")]
267+
pub fn into_pin(boxed: Box<T>) -> Pin<Box<T>> {
268+
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
269+
// when `T: !Unpin`, so it's safe to pin it directly without any
270+
// additional requirements.
271+
unsafe { Pin::new_unchecked(boxed) }
272+
}
260273
}
261274

262275
#[stable(feature = "rust1", since = "1.0.0")]
@@ -456,10 +469,7 @@ impl<T: ?Sized> From<Box<T>> for Pin<Box<T>> {
456469
///
457470
/// This conversion does not allocate on the heap and happens in place.
458471
fn from(boxed: Box<T>) -> Self {
459-
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
460-
// when `T: !Unpin`, so it's safe to pin it directly without any
461-
// additional requirements.
462-
unsafe { Pin::new_unchecked(boxed) }
472+
Box::into_pin(boxed)
463473
}
464474
}
465475

0 commit comments

Comments
 (0)