【发布时间】:2020-01-29 19:16:57
【问题描述】:
我希望我的未来能够睡一个“框架”,这样其他工作就可以进行。这是这个想法的有效实现吗?
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
}
}
}
具体来说,我担心的是,如果在投票返回Pending 之前进行了wake_by_ref 调用,上下文将不会“注意到”它。 poll 的接口契约是否保证此任务在以这种方式执行时会立即重新轮询?
【问题讨论】:
标签: asynchronous rust future