| 1 |
use core::pin::Pin; |
| 2 |
use futures_core::future::Future; |
| 3 |
use futures_core::task::{Context, Poll}; |
| 4 |
|
| 5 |
/// A macro which yields to the event loop once. |
| 6 |
/// |
| 7 |
/// This is equivalent to returning [`Poll::Pending`](futures_core::task::Poll) |
| 8 |
/// from a [`Future::poll`] implementation. |
| 9 |
/// Similarly, when using this macro, it must be ensured that [`wake`](std::task::Waker::wake) |
| 10 |
/// is called somewhere when further progress can be made. |
| 11 |
/// |
| 12 |
/// This macro is only usable inside of async functions, closures, and blocks. |
| 13 |
/// It is also gated behind the `async-await` feature of this library, which is |
| 14 |
/// activated by default. |
| 15 |
#[macro_export] |
| 16 |
macro_rules! pending { |
| 17 |
() => { |
| 18 |
$crate::__private::async_await::pending_once().await |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
#[doc(hidden)] |
| 23 |
pub fn pending_once() -> PendingOnce { |
| 24 |
PendingOnce { is_ready: false } |
| 25 |
} |
| 26 |
|
| 27 |
#[allow(missing_debug_implementations)] |
| 28 |
#[doc(hidden)] |
| 29 |
pub struct PendingOnce { |
| 30 |
is_ready: bool, |
| 31 |
} |
| 32 |
|
| 33 |
impl Future for PendingOnce { |
| 34 |
type Output = (); |
| 35 |
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> { |
| 36 |
if self.is_ready { |
| 37 |
Poll::Ready(()) |
| 38 |
} else { |
| 39 |
self.is_ready = true; |
| 40 |
Poll::Pending |
| 41 |
} |
| 42 |
} |
| 43 |
} |