rust - 没有为实现 `poll` 的类型找到名为 `Future` 的方法

标签 rust future

我正在尝试创建一个允许某人调用 .shutdown() 的结构,这将解决 future (否则未决)。它只能被调用一次。在 Future 特征的实现中,我收到一个错误,指出 poll 没有定义,尽管它存在于 the documentation 中。 (在 impl Future 下)。

尽管我使用 std::future::Future 作为 impl,但我尝试添加 use futures::prelude::* ,这会将预览特征纳入范围。 RLS 和 rustc 都通知我导入未使用,所以这不是问题。

请注意,我没有使用一个简单的 bool 标志,因为我打算让它能够从任何线程调用——这是一个与这里无关的实现细节。

use futures::channel::oneshot; // futures-preview@0.3.0-alpha.17
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

pub struct ShutdownHandle {
    sender: oneshot::Sender<()>,
    receiver: oneshot::Receiver<()>,
}

impl ShutdownHandle {
    pub fn new() -> Self {
        let (sender, receiver) = oneshot::channel();
        Self { sender, receiver }
    }

    pub fn shutdown(self) -> Result<(), ()> {
        self.sender.send(())
    }
}

impl Future for ShutdownHandle {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        self.receiver.poll(&mut cx).map(|_| ())
    }
}

fn main() {
    let runner = ShutdownHandle::new();
    assert!(runner.shutdown().is_ok());
}

我收到以下错误:

error[E0599]: no method named `poll` found for type `futures_channel::oneshot::Receiver<()>` in the current scope
  --> src/main.rs:28:23
   |
28 |         self.receiver.poll(&mut cx).map(|_| ())
   |                       ^^^^

我错过了什么?当然有一些方法可以“通过”投票。我每晚都在使用 (2019-07-18)。

最佳答案

这是真的,Receiver不执行 Future ;只有 Pin<&mut Receiver> 确实如此。您需要将类型的固定投影到字段。

当底层类型可能未实现时 Unpin

impl Future for ShutdownHandle {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        // I copied this code from Stack Overflow without reading the text that
        // told me how to verify that this code uses `unsafe` correctly.
        unsafe { self.map_unchecked_mut(|s| &mut s.receiver) }.poll(cx).map(|_| ())
    }
}

您必须阅读 pin module彻底了解使用要求 unsafe在这里。

更清洁的解决方案

我喜欢使用辅助库,例如 pin_project ,处理更复杂的投影类型:

#[unsafe_project(Unpin)]
pub struct ShutdownHandle {
    #[pin]
    sender: oneshot::Sender<()>,
    #[pin]
    receiver: oneshot::Receiver<()>,
}

impl Future for ShutdownHandle {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let this = self.project();
        this.receiver.poll(cx).map(|_| ())
    }
}

当基础类型实现Unpin

Ömer Erden points out future 预览 crate 提供 FutureExt::poll_unpin .此方法采用对实现 Unpin 的类型的可变引用。并创建一个全新的 Pin用它。

oneshot::Receiver执行Unpin ,这可以在这里使用:

impl Future for ShutdownHandle {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        self.receiver.poll_unpin(cx).map(|_| ())
    }
}

另见

关于rust - 没有为实现 `poll` 的类型找到名为 `Future` 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369123/

相关文章:

rust - 如何在实现特征时明确指定生命周期?

rust - Rust Gtk使用Glade文件从pixbuf设置了GtkImage

macos - OpenSSL crate 在 Mac OS X 10.11 上编译失败

scala - Scala 中的 Future 和无限循环

scala - 同步 2 个或更多 Future 的计算

stream - 如何通过 io::Write 特征写入来通过 futures Stream 发送数据?

mongodb - Play 和 ReactiveMongo 嵌套 future 响应的编译器错误

rust - 为什么有时需要 extern crate?

rust - 我需要任何 *variantType 标记吗?

Java future 和无限计算