【发布时间】: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)。
【问题讨论】: