【问题标题】:No method named `poll` found for a type that implements `Future`没有为实现“Future”的类型找到名为“poll”的方法
【发布时间】:2019-08-06 22:16:39
【问题描述】:

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

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

请注意,我没有使用简单的布尔标志,因为我希望它能够从任何线程调用 - 这是一个与此处无关的实现细节。

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)。

【问题讨论】:

    标签: rust future


    【解决方案1】:

    确实,Receiver 没有实现Future;只有 Pin&lt;&amp;mut Receiver&gt; 可以。您需要投影从您的类型到字段的固定。

    当底层类型可能无法实现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,futures-preview 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(|_| ())
        }
    }
    

    另见

    【讨论】:

    • 确实,Receiver 没有实现Future;只有 Pin 可以。 在这种情况下,使用poll_unpin 会是一个更快的解决方案,但不确定使用poll_unpin 是否有任何陷阱。我想知道它是否适用于这个问题?
    • @ÖmerErden 在这种特定情况下,这似乎是更简单的解决方案,并且避免使用unsafe(至少直接)。谢谢!
    • 供我自己和其他任何人将来参考:请记住,即使您正在实现该特征,您也可能会收到此错误,因为目前有从 0.1 到 0.3 的转换。希望这个过渡期会相对较短,但这是由于答案中链接的相同原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多