I'm trying to implement futures::stream::Stream
on a series of HTTP requests. I need something like long-polling (with the same request each time), but in asynchronous style.
I have declared a structure with a HTTP request future and in the Stream
implementation block I need to get the status of this Future
by calling its poll
method, but I get an error.
I've spend some time playing with Pin
, Box
and other containers but had no luck.
What I do wrong? How can I fix it? Is there anywhere an example of how can I do something similar?
I'm using crate futures = "0.3.1"
and here is a simplified version of my code:
use core::pin::Pin;
use futures::stream::Stream;
use std::{
future::Future,
task::{Context, Poll},
};
struct MyStruct {
some_future: Option<Pin<Box<dyn Send + Future<Output = Result<Vec<u8>, reqwest::Error>>>>>,
}
impl Stream for MyStruct {
type Item = u8;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.some_future {
Some(some_future) => {
let poll = some_future.poll(cx);
// ... some stuff to reinitialize self.some_future if it's Poll::Ready
poll
}
None => unimplemented!(),
}
}
}
fn main() {
println!("Hello, world!");
}
And here is the error I'm facing:
error[E0599]: no method named `poll` found for type `std::pin::Pin<std::boxed::Box<dyn core::future::future::Future<Output = std::result::Result<std::vec::Vec<u8>, reqwest::error::Error>> + std::marker::Send>>` in the current scope
--> src/main.rs:18:40
|
18 | let poll = some_future.poll(cx);
| ^^^^ method not found in `std::pin::Pin<std::boxed::Box<dyn core::future::future::Future<Output = std::result::Result<std::vec::Vec<u8>, reqwest::error::Error>> + std::marker::Send>>`