3

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!");
}

Playground

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>>`

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • It looks like your question might be answered by the answers of [Is there any way to create a async stream generator that yields the result of repeatedly calling a function?](https://stackoverflow.com/q/58700741/155423) and [No method named `poll` found for a type that implements `Future`](https://stackoverflow.com/q/57369123/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 13 '19 at 18:33
  • 2
    Wow, seems like the first question is completely suites my needs. Thank you! We can close the question then :) – iDeBugger Freeman Nov 13 '19 at 18:40

0 Answers0