为什么Handler trait没有实现简单的异步fn返回状态?



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

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)

这看起来几乎相同的文档中提供的一个例子:

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

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

最新更新