rust icon indicating copy to clipboard operation
rust copied to clipboard

Tracking Issue for `pin_deref_mut`

Open jonhoo opened this issue 4 years ago • 8 comments
trafficstars

Feature gate: #![feature(pin_deref_mut)]

This is a tracking issue for Pin::as_deref_mut.

The feature adds the method as_deref_mut to Pin, which allow a safe transformation from a Pin<&mut Pin<P>> to a Pin<&mut P::Target>.

Public API

impl<'a, P: DerefMut> Pin<&'a mut Pin<P>> {
    pub fn as_deref_mut(self) -> Pin<&'a mut P::Target> { /* .. */ }
}

Steps / History

  • [x] Implementation: #81363
  • [ ] Final comment period (FCP)
  • [ ] Stabilization PR

Unresolved Questions

  • None yet.

jonhoo avatar Jul 06 '21 23:07 jonhoo

from a Pin<P>

It's from a Pin<&'a mut Pin<P>>, right? The nesting is a bit odd. But I guess it is what comes up in practice?

RalfJung avatar Nov 07 '22 18:11 RalfJung

Ah, yes, sorry — fixed!

jonhoo avatar Nov 07 '22 22:11 jonhoo

Are there any remaining blockers on this? It's a pretty useful function for blanket impls for traits with poll methods (e.g. Future already uses it in its impl<P> Future for Pin<P> impl).

coolreader18 avatar Nov 16 '23 03:11 coolreader18

Not that I know of — given it touches the Pin machinery, I suspect it should go through an actual FCP, but I'll let others be the judge of that :)

jonhoo avatar Nov 18 '23 13:11 jonhoo

Currently this method is located in its own solitary impl block:

impl<'a, Ptr: DerefMut> Pin<&'a mut Pin<Ptr>> {
    pub fn as_deref_mut(self) -> Pin<&'a mut Ptr::Target>;
}

I'd like to consider changing it to:

impl<Ptr> Pin<Ptr> {
    pub fn as_deref_mut(self: Pin<&mut Pin<Ptr>>) -> Pin<&mut Ptr::Target>
    where
        Ptr: DerefMut;
}

Some advantages:

  • Synergy with the existing as_ref and as_mut signatures (stable since Rust 1.33)
  • Lifetime elision reduces noise in the signature
  • Turbofish less verbose: Pin::<&mut T>::as_deref_mut vs Pin::<&mut Pin<&mut T>>::as_deref_mut
impl<Ptr> Pin<Ptr> {
    pub fn as_ref(&self) -> Pin<&Ptr::Target>
    where
        Ptr: Deref;

    pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target>
    where
        Ptr: DerefMut;
}

dtolnay avatar Jun 25 '24 16:06 dtolnay

Any concern blocks this from stablization?

thynson avatar Jul 11 '24 07:07 thynson

@dtolnay I agree — that is better :+1:

jonhoo avatar Jul 15 '24 19:07 jonhoo

Would it be possible to start an FCP on this?

coolreader18 avatar Aug 26 '24 02:08 coolreader18

@rust-lang/libs-api: @rfcbot fcp merge

.as_deref_mut() is like .get_mut().as_mut(), but without enforcing an unnecessary Ptr: Unpin. It is like unsafe { .get_unchecked_mut() }.as_mut(), but is unconditionally safe.

The use case arises in real-world code when you are implementing a trait that takes self: Pin<&mut Self> where the Self type of your impl is Pin<Ptr>. So what you have is something like Pin<&mut Pin<Box<T>>>, and want to delegate to the impl for T which takes Pin<&mut T>. Some examples:

In tokio: impl<P> AsyncRead for Pin<P>

In hyper: impl<P> Read for Pin<P>

In futures: impl<P> Stream for Pin<P>

In actix: impl<P, A> ActorFuture<A> for Pin<P>

In libcore:

https://github.com/rust-lang/rust/blob/bf662eb80838008acabc307dd64d84935ce3a20d/library/core/src/future/future.rs#L116-L122

https://github.com/rust-lang/rust/blob/bf662eb80838008acabc307dd64d84935ce3a20d/library/core/src/async_iter/async_iter.rs#L99-L106

dtolnay avatar Aug 27 '24 06:08 dtolnay

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

  • [x] @Amanieu
  • [x] @BurntSushi
  • [x] @dtolnay
  • [ ] @joshtriplett
  • [ ] @m-ou-se

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

rfcbot avatar Aug 27 '24 06:08 rfcbot

:bell: This is now entering its final comment period, as per the review above. :bell:

rfcbot avatar Sep 24 '24 14:09 rfcbot

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

rfcbot avatar Oct 04 '24 14:10 rfcbot