From d1a42ea8d05b00a3b05f35531c8c78e8c7c6bde9 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Thu, 3 Jan 2019 21:04:35 +0100 Subject: [PATCH] Add discoverable function for converting Box -> Pin> --- src/liballoc/boxed.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 80e259685ab..1c459f5c425 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -257,6 +257,19 @@ impl Box { { unsafe { &mut *Box::into_raw(b) } } + + /// Converts a `Box` into a `Pin>` + /// + /// This conversion does not allocate on the heap and happens in place. + /// + /// This is also available via [`From`]. + #[unstable(feature = "box_into_pin", issue = "0")] + pub fn into_pin(boxed: Box) -> Pin> { + // It's not possible to move or replace the insides of a `Pin>` + // when `T: !Unpin`, so it's safe to pin it directly without any + // additional requirements. + unsafe { Pin::new_unchecked(boxed) } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -456,10 +469,7 @@ impl From> for Pin> { /// /// This conversion does not allocate on the heap and happens in place. fn from(boxed: Box) -> Self { - // It's not possible to move or replace the insides of a `Pin>` - // when `T: !Unpin`, so it's safe to pin it directly without any - // additional requirements. - unsafe { Pin::new_unchecked(boxed) } + Box::into_pin(boxed) } }