2个不稳定版本

0.2.0 2023年4月13日
0.1.0 2023年4月12日

#1230 in 异步

MPL-2.0许可证

14KB
119 代码行

enjoin

on crates.io on docs.rs

enjoin的异步连接宏在语法级别运行。它允许您...

breakcontinuereturn从运行在连接中的异步代码中退出

for _ in 0..10 {
    enjoin::join!(
        {
            if do_thing_1().await {
                break;
            }
        },
        {
            if do_thing_2().await {
                continue;
            }
        }
    );
}

在连接中使用?(尝试操作符)

async fn fetch_and_save_both() -> Result<(), Box<dyn Error>> {
    enjoin::join!(
        {
            let data = fetch_data_1().await?;
            save_data(data).await?;
        },
        {
            let data = fetch_data_2().await?;
            save_data(data).await?;
        }
    );
}

在连接中共享可变借用

...只要可变借用不会跨越yield点/await点。

let mut count = 0;
enjoin::join_auto_borrow!(
    {
        loop {
            incr_signal.next().await;
            count += 1;
        }
    },
    {
        loop {
            decr_signal.next().await;
            count -= 1;
        }
    }
);

关于动机、工作机制、与其他连接宏的比较等,请参见我的博客文章这里

依赖关系

~280–730KB
~17K SLoC