I have a structure (in my case,
tokio::fs::ReadDir) that implements an asynchronous method (
next_entry()) that returns
impl Future. I would like to manually wrap this structure in another structure which implements
Stream. In my case the easy answer is to use
ReadDir::poll_next_entry(), but in the more general case I am curious as to whether what I want to do is actually possible without using
unsafe. This post, answered by @alice, seems to imply that
unsafe is required, but addresses a slightly different scenario using raw pointers.
This code on Rust playground uses
std::mem::transmute() to extend the lifetime of the pinned future. Comment out the transmute on lines 36-38 and the compiler, "cannot infer an appropriate lifetime for lifetime parameter" for the pinned future.
My question is:
- Is this use of
unsafecorrect? My reasoning is that the future's lifetime is bound by the lifetime of
self.names, which is bound, by the lifetime of
self, which is
'a. But because
Pin<&'_ mut Self>the compiler thinks the lifetime of self is
'_and we must use
mem::transmute()to extend it.
- Is there a preferable way to do this without