小编Iv4*_*v4n的帖子

Rust 将加密和压缩操作改为异步的目的是什么?

在我的理解中,异步只能处理I/O密集型任务,例如读写套接字或文件,而无法处理CPU密集型任务,例如加密和压缩。

所以在 Rust Tokio Runtime 中,我认为只需要用来spawn_blocking处理 CPU 密集型任务。但我看过这个回购协议,例子是

#[tokio_02::main]
async fn main() -> Result<()> {
    let data = b"example";
    let compressed_data = compress(data).await?;
    let de_compressed_data = decompress(&compressed_data).await?;
    assert_eq!(de_compressed_data, data);
    println!("{:?}", String::from_utf8(de_compressed_data).unwrap());
    Ok(())
}
Run Code Online (Sandbox Code Playgroud)

该库在压缩和异步 I/O 类型之间创建适配器。

我有 3 个问题:

  1. 等待压缩/解压缩的目的是什么?

  2. 这些适配器是必要的还是我对异步的理解错误?

  3. 我可以直接在 Tokio 多线程运行时进行压缩操作吗?像这样

async fn foo() {
    let mut buf = ...;
    compress_sync(&mut buf);
    async_tcp_stream.write(buf).await;
}
Run Code Online (Sandbox Code Playgroud)

compression encryption asynchronous rust rust-tokio

0
推荐指数
1
解决办法
215
查看次数

标签 统计

asynchronous ×1

compression ×1

encryption ×1

rust ×1

rust-tokio ×1