相关疑难解决方法(0)

如何实现轮询异步 fn 的 Future 或 Stream?

我有一个Test我想实现的结构体std::future::Future,它会轮询function

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

struct Test;

impl Test {
    async fn function(&mut self) {}
}

impl Future for Test {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.function() {
            Poll::Pending => Poll::Pending,
            Poll::Ready(_) => Poll::Ready(()),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那没有用:

error[E0308]: mismatched types
  --> src/lib.rs:17:13
   |
10 |     async fn function(&mut self) {}
   |                                  - the `Output` of this `async fn`'s expected opaque …
Run Code Online (Sandbox Code Playgroud)

asynchronous unsafe future rust async-await

5
推荐指数
1
解决办法
2151
查看次数

标签 统计

async-await ×1

asynchronous ×1

future ×1

rust ×1

unsafe ×1