Niko Matsakis 和 Tyler Mandry 代表 Async 工作组发布了一篇文章,阐述了在 2022 年异步 Rust 的改进计划。
Rust 2024 Edition 中使用异步 Rust 的愿景
文章中先假设了一个 Rust 2024 edtion 中实现的异步愿景。
假设在 Rust 2024 Edition 中,你使用异步 Rust 创建一个新项目:使用crates.io 的第三方库 crabbycat
来遍历指定 GitHub 仓库的issues列表:
async fn main() {
for await? issue in crabbycat::issues("https://github.com/rust-lang/rust") {
if meets_criteria(&issue) {
println!("{issue:?}");
}
}
}
你的项目似乎工作正常,这个时候有人给你发起 PR: 增加 GitLab 支持。你看到 PR 的代码有下面改进:
// 增加了 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 代码应该像编写同步代码一样简单,除了偶尔出现的async
和await
关键字。
为此,Async 工作组组织成许多不同的计划,每个计划都在追求愿景的一部分:
async fn
的难题。过去一年的工作:async fn
,用于支持静态分发,即,可以返回impl Trait
。AsyncRead
和AsyncWrite
。yield
点之前被移动时的捕获分析。must_not_suspend
lint,用于捕获一些不应该跨 await
存活的值。上面这些计划都可以在 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