diff options
| author | kennytm <kennytm@gmail.com> | 2019-01-05 23:56:58 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-05 23:56:58 +0800 |
| commit | 676b0b0cc4f3a9801051696b3f7b88e75c7b9764 (patch) | |
| tree | 15fda102a08490d9be9ef401bab9a841e4a5fb7c /src | |
| parent | e1a1ab08361213e545136c51c52144acff1678d0 (diff) | |
| parent | d1a42ea8d05b00a3b05f35531c8c78e8c7c6bde9 (diff) | |
| download | rust-676b0b0cc4f3a9801051696b3f7b88e75c7b9764.tar.gz rust-676b0b0cc4f3a9801051696b3f7b88e75c7b9764.zip | |
Rollup merge of #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 #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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/liballoc/boxed.rs | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index fbd0b948b82..1c459f5c425 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -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")] @@ -451,15 +464,12 @@ impl<T> From<T> for Box<T> { } #[stable(feature = "pin", since = "1.33.0")] -impl<T> From<Box<T>> for Pin<Box<T>> { +impl<T: ?Sized> From<Box<T>> for Pin<Box<T>> { /// Converts a `Box<T>` into a `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) } } |
