1 use core::{ 2 future::Future, 3 pin::Pin, 4 task::{Context, Poll}, 5 }; 6 7 /// A small future that yields once and then returns. 8 #[derive(Default)] 9 pub struct Yield { 10 yielded: bool, 11 } 12 13 impl Yield { 14 /// Create a new `Yield`. new() -> Self15 pub fn new() -> Self { 16 Self::default() 17 } 18 } 19 20 impl Future for Yield { 21 type Output = (); 22 poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>23 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { 24 if self.yielded { 25 Poll::Ready(()) 26 } else { 27 // Flag ourselves as yielded to return next time, and also 28 // flag the waker that we're already ready to get 29 // re-enqueued for another poll. 30 self.yielded = true; 31 cx.waker().wake_by_ref(); 32 Poll::Pending 33 } 34 } 35 } 36