相关疑难解决方法(0)

在轮询期间唤醒 Rust 未来是否有效?

我希望能够为一个“框架”睡我的未来,以便其他工作可以发生。这是这个想法的有效实现吗?

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如果调用是在轮询返回之前进行的,则上下文不会“注意到”该调用Pendingpoll以这种方式执行时,接口契约是否保证此任务会立即重新轮询?

asynchronous future rust

9
推荐指数
1
解决办法
669
查看次数

标签 统计

asynchronous ×1

future ×1

rust ×1