1 use std::future::Future;
2 use std::pin::Pin;
3 use std::task::{Context, Poll};
4 
5 cfg_rt! {
6     /// Yields execution back to the Tokio runtime.
7     ///
8     /// A task yields by awaiting on `yield_now()`, and may resume when that
9     /// future completes (with no output.) The current task will be re-added as
10     /// a pending task at the _back_ of the pending queue. Any other pending
11     /// tasks will be scheduled. No other waking is required for the task to
12     /// continue.
13     ///
14     /// See also the usage example in the [task module](index.html#yield_now).
15     #[must_use = "yield_now does nothing unless polled/`await`-ed"]
16     pub async fn yield_now() {
17         /// Yield implementation
18         struct YieldNow {
19             yielded: bool,
20         }
21 
22         impl Future for YieldNow {
23             type Output = ();
24 
25             fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
26                 if self.yielded {
27                     return Poll::Ready(());
28                 }
29 
30                 self.yielded = true;
31                 cx.waker().wake_by_ref();
32                 Poll::Pending
33             }
34         }
35 
36         YieldNow { yielded: false }.await
37     }
38 }
39