diff --git a/library/core/src/future/future.rs b/library/core/src/future/future.rs index 15952c6806fa4..7cd0ce9d2efe0 100644 --- a/library/core/src/future/future.rs +++ b/library/core/src/future/future.rs @@ -111,11 +111,11 @@ impl Future for &mut F { #[stable(feature = "futures_api", since = "1.36.0")] impl

Future for Pin

where - P: Unpin + ops::DerefMut, + P: ops::DerefMut, { type Output = <

::Target as Future>::Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::get_mut(self).as_mut().poll(cx) + ::poll(self.as_deref_mut(), cx) } } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 839be5a143f71..f7d48a91396ff 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -128,6 +128,7 @@ #![feature(exhaustive_patterns)] #![feature(no_core)] #![feature(auto_traits)] +#![feature(pin_deref_mut)] #![feature(prelude_import)] #![feature(ptr_metadata)] #![feature(repr_simd, platform_intrinsics)] diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 3d888299485b5..6b1a12ed18c0f 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -802,6 +802,44 @@ impl Pin<&'static T> { } } +impl<'a, P: DerefMut> Pin<&'a mut Pin

> { + /// Gets a pinned mutable reference from this nested pinned pointer. + /// + /// This is a generic method to go from `Pin<&mut Pin>>` to `Pin<&mut T>`. It is + /// safe because the existence of a `Pin>` ensures that the pointee, `T`, cannot + /// move in the future, and this method does not enable the pointee to move. "Malicious" + /// implementations of `P::DerefMut` are likewise ruled out by the contract of + /// `Pin::new_unchecked`. + #[unstable(feature = "pin_deref_mut", issue = "86918")] + #[inline(always)] + pub fn as_deref_mut(self) -> Pin<&'a mut P::Target> { + // SAFETY: What we're asserting here is that going from + // + // Pin<&mut Pin

> + // + // to + // + // Pin<&mut P::Target> + // + // is safe. + // + // We need to ensure that two things hold for that to be the case: + // + // 1) Once we give out a `Pin<&mut P::Target>`, an `&mut P::Target` will not be given out. + // 2) By giving out a `Pin<&mut P::Target>`, we do not risk of violating `Pin<&mut Pin

>` + // + // The existence of `Pin

` is sufficient to guarantee #1: since we already have a + // `Pin

`, it must already uphold the pinning guarantees, which must mean that + // `Pin<&mut P::Target>` does as well, since `Pin::as_mut` is safe. We do not have to rely + // on the fact that P is _also_ pinned. + // + // For #2, we need to ensure that code given a `Pin<&mut P::Target>` cannot cause the + // `Pin

` to move? That is not possible, since `Pin<&mut P::Target>` no longer retains + // any access to the `P` itself, much less the `Pin

`. + unsafe { self.get_unchecked_mut() }.as_mut() + } +} + impl Pin<&'static mut T> { /// Get a pinned mutable reference from a static mutable reference. ///