我希望能够为一个“框架”睡我的未来,以便其他工作可以发生。这是这个想法的有效实现吗?
use std::future::Future;
use std::task::{Context, Poll};
use std::pin::Pin;
struct Yield {
yielded: bool,
}
impl Future for Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<()> {
if self.yielded {
Poll::Ready(())
} else {
self.yielded = true;
// This is the part I'm concerned about
ctx.waker().wake_by_ref();
Poll::Pending
}
}
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我担心的是,wake_by_ref如果调用是在轮询返回之前进行的,则上下文不会“注意到”该调用Pending。poll以这种方式执行时,接口契约是否保证此任务会立即重新轮询?