Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Safe API for converting `&mut Box` to `&mut Pin>`?
Hi everyone,
I'm working with futures::Sink
and make a study about Pin<Box<dyn Sink>>
between Box<dyn Sink>
. I'm into a situation where I need to convert a &mut Box<dyn Sink>
to &mut Pin<Box<dyn Sink>>
. I believe this conversion should be safe, but I can't find a safe API to do it.
Here's why I think this conversion should be safe:
&mut Box<T>
reference to the boxed value&mut Box<T>
reference, so I can't use it to move the contents afterward&mut Pin<Box<T>>
, I cannot safely obtain &mut T
(unless T: Unpin
) to move the contentsThe key insight is that while Box<T>
puts T
on the heap, I could theoretically still move T
's contents through operations like std::mem::replace(&mut **box_ref, new_value)
. However, once I convert to &mut Pin<Box<T>>
and lose access to the original reference, there's no way to perform such operations anymore.
I'm wondering why there isn't a API just like Option::as_pin_mut()
for Box<T>
.
The conversion seems logically sound to me - the borrowing rules ensure I can't access the original Box
after conversion, and Pin
prevents unsafe access to the contents. But I'm curious if there are deeper reasons why this isn't available as a safe API.
Any insights would be greatly appreciated!
2 posts - 2 participants
🏷️ Rust_feed