I am attempting to create a struct that will allow someone to call .shutdown()
, which will resolve a future (that is otherwise pending). It can only be called once. In the implementation of the Future
trait, I receive an error that poll
is not defined, despite it being present in the documentation (under impl Future
).
Though I am using std::future::Future
as the impl
, I tried adding use futures::prelude::*
, which would bring the preview trait into scope. Both RLS and rustc inform me that the import is unused, so that's not the issue.
Note that I am not using a simple boolean flag, as I intend for this to be able to be callable from any thread — that's an implementation detail that is irrelevant here.
use futures::channel::oneshot; // [email protected]
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());
}
I receive the following error:
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(|_| ())
| ^^^^
What am I missing? Surely there's some way to "pass through" the polling. I am using nightly (2019-07-18).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…