async-await: fix build for latest nightly (#940)

Fixes: #936
This commit is contained in:
Carl Lerche
2019-03-01 15:40:42 -08:00
committed by GitHub
parent db4019d84a
commit 85e3bd34af
3 changed files with 24 additions and 74 deletions
+23 -3
View File
@@ -1,8 +1,28 @@
use std::future::Future as StdFuture;
use std::pin::Pin;
use std::task::{Poll, Waker};
async fn map_ok<T: StdFuture>(future: T) -> Result<(), ()> {
let _ = await!(future);
Ok(())
fn map_ok<T: StdFuture>(future: T) -> impl StdFuture<Output = Result<(), ()>> {
MapOk(future)
}
struct MapOk<T>(T);
impl<T> MapOk<T> {
fn future<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut T> {
unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }
}
}
impl<T: StdFuture> StdFuture for MapOk<T> {
type Output = Result<(), ()>;
fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
match self.future().poll(waker) {
Poll::Ready(_) => Poll::Ready(Ok(())),
Poll::Pending => Poll::Pending,
}
}
}
/// Like `tokio::run`, but takes an `async` block