gpt4 book ai didi

rust - 为什么没有为简单的异步 fn 返回状态实现处理程序特征?

转载 作者:行者123 更新时间:2023-12-03 08:04:17 25 4
gpt4 key购买 nike

我认为应该为返回 StatusCodeasync 函数实现 Handler:

use axum::{
headers,
http::StatusCode,
routing::{delete, get, post, put},
Router,
};

pub fn create_routes() -> Router {
Router::new().route("/webhook", post(webhook_handler()))
}

#[debug_handler]
async fn webhook_handler() -> StatusCode {
StatusCode::OK
}
error[E0277]: the trait bound `impl futures::Future<Output = StatusCode>: Handler<_, _>` is not satisfied
--> src/webhook.rs:16:39
|
16 | Router::new().route("/webhook", post(webhook_handler()))
| ---- ^^^^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for `impl futures::Future<Output = StatusCode>`
| |
| required by a bound introduced by this call
|
= help: the trait `Handler<T, ReqBody>` is implemented for `Layered<S, T>`
note: required by a bound in `axum::routing::post`
--> /home/andrew/.cargo/registry/src/github.com-1ecc6299db9ec823/axum-0.5.11/src/routing/method_routing.rs:400:1
|
400 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `axum::routing::post`
= note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)

这看起来与 the documentation 中提供的示例几乎相同:

// `StatusCode` gives an empty response with that status code
async fn status() -> StatusCode {
StatusCode::NOT_FOUND
}

最佳答案

函数本身需要作为参数传递给 post,而不是函数调用。删除了括号,如下所示: .route("webhook", post(webhook_handler))

关于rust - 为什么没有为简单的异步 fn 返回状态实现处理程序特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72918729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com