不能在 Rust 的异步函数中使用await

问题描述 投票:0回答:1

我有这个代码


#[api_operation(
    tag = "image",
    summary = "uploads an image",
    description = r###"upload an image to an account"###,
    error_code = 405
)]
pub async fn upload_image(mut payload: Multipart) -> impl Responder {
    let upload_dir = std::env::var("UPLOAD_DIR").unwrap_or_else(|_| "./uploads".to_string());

    let upload_status = files::save_file(payload, format!("{upload_dir}/test.png").to_string()).await;

对我来说这看起来像是一个

async
方法。

但由于某种原因我遇到编译错误

error[E0728]: `await` is only allowed inside `async` functions and blocks
  --> src/image.rs:18:97
   |
9  | / #[api_operation(
10 | |     tag = "image",
11 | |     summary = "uploads an image",
12 | |     description = r###"upload an image to an account"###,
13 | |     error_code = 405
14 | | )]
   | |__- this is not `async`
...
18 |       let upload_status = files::save_file(payload, format!("{upload_dir}/test.png").to_string()).await;
   |                                                                                                   ^^^^^ only allowed inside `async` functions and blocks

是说宏导致它不是异步的,还是为什么我不能使用await?

有什么办法可以解决吗?

asynchronous rust
1个回答
0
投票

这是 apistos::api_operation 宏中的一个

bug
,现已解决,您的代码确实可以与包含相应 代码更改的版本一起使用

© www.soinside.com 2019 - 2024. All rights reserved.