Converting Pin<&T> to &T

Is it always safe to convert Pin<&T> to &T via into_inner_unchecked? Given that the object cannot be moved from via &T, I would assume so. I am a bit surprised the standard library does not expose a safe function to do this, so I wanted to make sure I get this right.

1 Like

There is a safe function for this: get_ref()


Pin implements Deref, so you can even call functions that expect &T or methods of T directly on Pin<&T>.


One notable difference is that … ah it says so in the docs of get_ref() anyways

Note: Pin also implements Deref to the target, which can be used to access the inner value. However, Deref only provides a reference that lives for as long as the borrow of the Pin, not the lifetime of the Pin itself. This method allows turning the Pin into a reference with the same lifetime as the original Pin.

i.e. the difference is that get_ref turns Pin<&'a T> into &'a T, and with the Copy impl, this means that you can also turn &'b Pin<&'a T> into &'a T – whereas Deref turns &'b Pin<&'a T> into &'b T.