Skip to content

Commit

Permalink
Rollup merge of rust-lang#97397 - JohnTitor:stabilize-box-into-pin, r…
Browse files Browse the repository at this point in the history
…=Mark-Simulacrum

Stabilize `box_into_pin`

FCP has been completed: rust-lang#62370 (comment)
Also, adds notes as per rust-lang#62370 (comment)
Closes rust-lang#62370
  • Loading branch information
Dylan-DPC authored Jun 2, 2022
2 parents 0ed450b + 572c390 commit 78cd67b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,14 +1175,33 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
/// This conversion does not allocate on the heap and happens in place.
///
/// This is also available via [`From`].
#[unstable(feature = "box_into_pin", issue = "62370")]
///
/// # Notes
///
/// It's not recommended that crates add an impl like `From<Box<T>> for Pin<T>`,
/// as it'll introduce an ambiguity when calling `Pin::from`.
/// A demonstration of such a poor impl is shown below.
///
/// ```compile_fail
/// # use std::pin::Pin;
/// struct Foo; // A type defined in this crate.
/// impl From<Box<()>> for Pin<Foo> {
/// fn from(_: Box<()>) -> Pin<Foo> {
/// Pin::new(Foo)
/// }
/// }
///
/// let foo = Box::new(());
/// let bar = Pin::from(foo);
/// ```
#[stable(feature = "box_into_pin", since = "1.63.0")]
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
pub const fn into_pin(boxed: Self) -> Pin<Self>
where
A: 'static,
{
// 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
// when `T: !Unpin`, so it's safe to pin it directly without any
// additional requirements.
unsafe { Pin::new_unchecked(boxed) }
}
Expand Down

0 comments on commit 78cd67b

Please sign in to comment.