Skip to content

Commit

Permalink
Add discoverable function for converting Box<T> -> Pin<Box<T>>
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Jan 3, 2019
1 parent 5e3a560 commit d1a42ea
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/liballoc/boxed.rs
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d1a42ea

Please sign in to comment.