error[E0599]: no method named `peek` found for type `&mut futures_util::stream::peek::Peekable<futures_channel::mpsc::Receiver<i32>>` in the current scope
--> src/main.rs:7:36
|
7 | block_on(poll_fn(|cx| peekable.peek(cx)));
| ^^^^ method not found in `&mut futures_util::stream::peek::Peekable<futures_channel::mpsc::Receiver<i32>>`
My guess is thats its due to peek's signature which takes a self: Pin<&mut Peekable<St> as opposed to a &mut self, but I'm not sure.
fn should_compile(peekable: Pin<&mut Peekable<mpsc::Receiver<i32>>>) {
let fut = poll_fn(|cx| peekable.peek(cx));
block_on(fut);
}
Errors with:
error[E0507]: cannot move out of `peekable`, a captured variable in an `FnMut` closure
--> src/main.rs:9:28
|
8 | fn should_compile(peekable: Pin<&mut Peekable<mpsc::Receiver<i32>>>) {
| -------- captured outer variable
9 | let fut = poll_fn(|cx| peekable.peek(cx));
| ^^^^^^^^ move occurs because `peekable` has type `std::pin::Pin<&mut futures_util::stream::peek::Peekable<futures_channel::mpsc::Receiver<i32>>>`, which does not implement the `Copy` trait
From what I understand this is due to the fact that futures::future::poll_fn is a FnMut which could be called multiple times, requiring moving the Pin each time. However, neither the Pin nor the &mut Peekable<...> are copyable.
I think the closure, returning a future means it really needs to own the data that goes into it. Especially if it needs to be Pin, since it can't be moved.
What you can do potentially is to move the poll_fn up the stack, so that this method is inside of it:
I'm not quite sure what you try to achieve, so maybe it's no help, but this will compile.
This won't work unfortunately.
I think the closure, returning a future means it really needs to own the data that goes into it. Especially if it needs to be Pin, since it can't be moved.
I do agree that the issue seems to be about the ownership of the Pin.
Looking at the futures repo, the only use of Peekable::peek is in a Future implementation, which is weirdly specific. So its possible that's its only meant to be used in this context.
I'd say it might be worth that I file a github issue on the futures github, and at least improve the function's doc.
for sure as it takes a context it's meant to be used inside a poll function. I have never used it before so I don't really know what the reasoning behind Peekable is.
Of course you can do what this test code does and create a custom future to wrap it, but it really depends on what you are trying to do.
Note that this test code looks quite old, as it doesn't pin self.inner and doesn't take self by pin, so I doubt this code still works as shown.
I agree that more explanation and examples of intended use would be helpful.