Skip to content

Commit 676b0b0

Browse files
authored
Rollup merge of rust-lang#57313 - Nemo157:box-to-pin, r=cramertj
Improve Box<T> -> Pin<Box<T>> conversion I found the `From` trait conversion for this very hard to find, having a named function for it is much more discoverable. Also fixes rust-lang#56256 as I need that in the place I'm using this. Has a placeholder tracking issue, will file an issue once I get feedback.
2 parents e1a1ab0 + d1a42ea commit 676b0b0

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/liballoc/boxed.rs

+15-5
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")]
@@ -451,15 +464,12 @@ impl<T> From<T> for Box<T> {
451464
}
452465

453466
#[stable(feature = "pin", since = "1.33.0")]
454-
impl<T> From<Box<T>> for Pin<Box<T>> {
467+
impl<T: ?Sized> From<Box<T>> for Pin<Box<T>> {
455468
/// Converts a `Box<T>` into a `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)