| 1 | use crate::future::FutureExt; |
| 2 | use core::pin::Pin; |
| 3 | use futures_core::future::Future; |
| 4 | use futures_core::task::{Context, Poll}; |
| 5 | |
| 6 | /// A macro which returns the result of polling a future once within the |
| 7 | /// current `async` context. |
| 8 | /// |
| 9 | /// This macro is only usable inside of `async` functions, closures, and blocks. |
| 10 | /// It is also gated behind the `async-await` feature of this library, which is |
| 11 | /// activated by default. |
| 12 | /// |
| 13 | /// If you need the result of polling a [`Stream`](crate::stream::Stream), |
| 14 | /// you can use this macro with the [`next`](crate::stream::StreamExt::next) method: |
| 15 | /// `poll!(stream.next())`. |
| 16 | #[macro_export ] |
| 17 | macro_rules! poll { |
| 18 | ($x:expr $(,)?) => { |
| 19 | $crate::__private::async_await::poll($x).await |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | #[doc (hidden)] |
| 24 | pub fn poll<F: Future + Unpin>(future: F) -> PollOnce<F> { |
| 25 | PollOnce { future } |
| 26 | } |
| 27 | |
| 28 | #[allow (missing_debug_implementations)] |
| 29 | #[doc (hidden)] |
| 30 | pub struct PollOnce<F: Future + Unpin> { |
| 31 | future: F, |
| 32 | } |
| 33 | |
| 34 | impl<F: Future + Unpin> Future for PollOnce<F> { |
| 35 | type Output = Poll<F::Output>; |
| 36 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 37 | Poll::Ready(self.future.poll_unpin(cx)) |
| 38 | } |
| 39 | } |
| 40 | |