基于Future实现Future :: stream :: Stream

时间:2019-11-13 18:29:03

标签: rust future

我正在尝试对一系列HTTP请求实施vector<Vertex> connectLines; void addClickPoint(HWND hWnd, int X, int Y) { connectLines.push_back(Vertex(X, Y)); InvalidateRect(hWnd, NULL, TRUE); } ... case WM_LBUTTONDOWN: { POINTS clickPoint = MAKEPOINTS(lParam); addClickPoint(hWnd, clickPoint.x, clickPoint.y); /* alternatively: addClickPoint(hWnd, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); */ } break; // optionally: case WM_MOUSEMOVE: { if (wParam & MK_LBUTTON) { POINTS clickPoint = MAKEPOINTS(lParam); addClickPoint(hWnd, clickPoint.x, clickPoint.y); /* alternatively: addClickPoint(hWnd, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); */ } } break; // case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); ... if (connectLines.size() > 1) { MoveToEx(hdc, connectLines[0].GetX(), connectLines[0].GetY(), NULL); for(size_t i = 1; i < connectLines.size(); ++i) LineTo(hdc, connectLines[i].GetX(), connectLines[i].GetY()); } ... EndPaint(hWnd, &ps); } break; 。我需要长轮询(每次都具有相同的请求)之类的东西,但要使用异步样式。

我已经声明了一个带有将来HTTP请求的结构,并且在futures::stream::Stream实现块中,我需要通过调用其Stream方法来获取此Future的状态,但是我得到了一个错误。

我花了一些时间玩pollPin和其他容器,但没有运气。

我做错了什么?我该如何解决?有什么例子可以说明我该如何做类似的事情?

我使用的是板条箱Box,这是我的代码的简化版本:

futures = "0.3.1"

Playground

这是我面临的错误:

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

0 个答案:

没有答案