Add discoverable function for converting Box<T> -> Pin<Box<T>>

This commit is contained in:
Wim Looman 2019-01-03 21:04:35 +01:00
parent 5e3a560299
commit d1a42ea8d0

View File

@ -257,6 +257,19 @@ impl<T: ?Sized> Box<T> {
{
unsafe { &mut *Box::into_raw(b) }
}
/// Converts a `Box<T>` into a `Pin<Box<T>>`
///
/// 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<T>) -> Pin<Box<T>> {
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
// 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<T: ?Sized> From<Box<T>> for Pin<Box<T>> {
///
/// This conversion does not allocate on the heap and happens in place.
fn from(boxed: Box<T>) -> Self {
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
// when `T: !Unpin`, so it's safe to pin it directly without any
// additional requirements.
unsafe { Pin::new_unchecked(boxed) }
Box::into_pin(boxed)
}
}