前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【官方】2022年异步Rust的改进计划

【官方】2022年异步Rust的改进计划

作者头像
张汉东
发布2022-03-29 08:27:14
6740
发布2022-03-29 08:27:14
举报
文章被收录于专栏:Rust 编程

Niko Matsakis 和 Tyler Mandry 代表 Async 工作组发布了一篇文章,阐述了在 2022 年异步 Rust 的改进计划。

Rust 2024 Edition 中使用异步 Rust 的愿景

文章中先假设了一个 Rust 2024 edtion 中实现的异步愿景。

假设在 Rust 2024 Edition 中,你使用异步 Rust 创建一个新项目:使用crates.io 的第三方库 crabbycat来遍历指定 GitHub 仓库的issues列表:

代码语言:javascript
复制
async fn main() {
    for await? issue in crabbycat::issues("https://github.com/rust-lang/rust") {
        if meets_criteria(&issue) {
            println!("{issue:?}");
        }
    }
}

你的项目似乎工作正常,这个时候有人给你发起 PR: 增加 GitLab 支持。你看到 PR 的代码有下面改进:

代码语言:javascript
复制

// 增加了 Async Trait `IssueProvider` 来抽象 issues 接口
// 兼容 GitHub/Gitlab/Gitothers
trait IssueProvider {
    // 这个 issues 方法只需要遍历issues即可
    // 所以需要是一个实现AsyncIterator的类型
    async fn issues(&mut self, url: &str)
        -> impl AsyncIterator<Item = Result<Issue, Err>>;
}

#[derive(Debug)]
struct Issue {
    number: usize,
    header: String,
    assignee: String,
}

// 使用独立的方法打印issues列表
// provider代表issues提供方是GitHub 还是 其他,比如 Gitlab
fn process_issues(provider: &mut dyn IssueProvider) {
    for await? issue in provider.issues("https://github.com/rust-lang/rust") {
        if meets_criteria(&issue) {
            println!("{issue:?}");
        }
    }
}

你合并了该 PR,一切OK。后面有人想将你的项目移植到 Chartreuse 操作系统,而 Chartreuse 操作系统是基于 Actor 模型的,并且有自己的自定义异步运行时。然而,你其实并不需要关心这些。因为你所有代码都能够无缝地将底层运行时实现切换到 Chartreuse 异步运行时。

在 2022 年将要完成的目标

看得出来,异步 Rust 在 2024 Edition 会更加的完善,使用起来更加方便。但是为了达到这个目标,2022年必须完成一些基础工作,并且可以预料到的是,在这个过程中,异步 Rust 的很多细节会发生大量变化,不出意外的话,生成器的语法应该会备受争议。但我们的整体愿景并不会改变:编写异步 Rust 代码应该像编写同步代码一样简单,除了偶尔出现的asyncawait关键字。

为此,Async 工作组组织成许多不同的计划,每个计划都在追求愿景的一部分:

  • 异步基础计划[1],由 tmandry[2]领导,聚焦于解决trait中支持async fn的难题。过去一年的工作:
    • 协调和支持 `GAT`[3]`impl Trait`[4] 计划。
    • 起草 RFC #3185[5],在 trait 中实现静态 async fn,用于支持静态分发,即,可以返回impl Trait
    • 致力于动态分发的设计,Niko 有一系列博文阐述相关内容:Dyn async traits[6]
    • 其他改进,比如Contexts and capabilities in Rust[7]
  • 异步迭代器计划[8],由estebank[9]领导,探索生成器和异步生成器。Estebank 为生成器制作了一个程序宏的原型,并呼吁讨论语法和其他细节。
  • 可移植性计划[10],在nrc[11]的带领下,探索如何让代码在运行时之间轻松移植[12],从标准化的特征开始,例如AsyncReadAsyncWrite
  • 抛光打磨(Polish)计划[13],由eholk[14]领导,专注于通过较小的变化来提高现有的能力,这些变化共同产生了巨大的影响。比如:
    • PR #91032[15],改进生成器当变量在 yield 点之前被移动时的捕获分析。
    • PR #92508[16],它生成了临时作用域的精确范围,进一步避免不必要的生成器捕获。
    • guswynn[17]提交了PR #88865[18],创建了 must_not_suspend lint,用于捕获一些不应该跨 await 存活的值。
    • 开始研究 async stack traces 如何更加可读,更有帮助。
  • 工具计划,由pnkfelix[19]领导,致力于支持异步生态系统中那些正在创建有趣的工具来支持异步Rust的开发者们,诸如:
    • Michael Woerister正在探索异步crashdump恢复[20],提供一种机制来恢复和检查基于crashdump的异步Rust程序的状态。
    • Eliza Weisman和其他许多人[21]最近宣布了他们0.1版本的`tokio-console`[22] (异步Rust程序的诊断和调试工具)。

上面这些计划都可以在 roadmap[23] 中找到。

你如何参与

如果你想参与,最好的路线是先从「打磨抛光计划」开始,在其页面上有["如何帮助 "部分](https://rust-lang.github.io/wg-async/vision/roadmap/polish.html#-how-to-help ""如何帮助 "部分")可以参考。

参考资料

[1]异步基础计划: https://rust-lang.github.io/async-fundamentals-initiative/

[2]tmandry: https://github.com/tmandry

[3]GAT: https://rust-lang.github.io/generic-associated-types-initiative/

[4]impl Trait: https://rust-lang.github.io/impl-trait-initiative/

[5]RFC #3185: https://rust-lang.github.io/rfcs/3185-static-async-fn-in-trait.html

[6]Dyn async traits: http://smallcultfollowing.com/babysteps//blog/2022/01/07/dyn-async-traits-part-7/

[7]Contexts and capabilities in Rust: https://tmandry.gitlab.io/blog/posts/2021-12-21-context-capabilities/

[8]异步迭代器计划: https://estebank.github.io/rust-iterator-item-syntax.html

[9]estebank: https://github.com/estebank

[10]可移植性计划: https://www.ncameron.org/blog/portable-and-interoperable-async-rust/

[11]nrc: https://github.com/nrc

[12]探索如何让代码在运行时之间轻松移植: https://www.ncameron.org/blog/portable-and-interoperable-async-rust/

[13]抛光打磨(Polish)计划: https://rust-lang.github.io/wg-async/vision/roadmap/polish.html

[14]eholk: https://github.com/eholk

[15]PR #91032: https://github.com/rust-lang/rust/pull/91032

[16]PR #92508: https://github.com/rust-lang/rust/pull/92508

[17]guswynn: https://github.com/guswynn

[18]PR #88865: https://github.com/rust-lang/rust/pull/88865

[19]pnkfelix: https://github.com/pnkfelix

[20]异步crashdump恢复: https://github.com/rust-lang/async-crashdump-debugging-initiative

[21]其他许多人: https://tokio.rs/blog/2021-12-announcing-tokio-console#thanks-to

[22]tokio-console: https://github.com/tokio-rs/console

[23]roadmap: https://rust-lang.github.io/wg-async/vision/roadmap.html

[24]官方原文: https://blog.rust-lang.org/inside-rust/2022/02/03/async-in-2022.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 觉学社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档