首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Handler特性没有为简单异步fn返回状态实现?

为什么Handler特性没有为简单异步fn返回状态实现?
EN

Stack Overflow用户
提问于 2022-07-09 03:27:10
回答 1查看 174关注 0票数 1

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

代码语言:javascript
复制
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
}
代码语言:javascript
复制
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)

这似乎与文献资料中提供的示例几乎相同。

代码语言:javascript
复制
// `StatusCode` gives an empty response with that status code
async fn status() -> StatusCode {
    StatusCode::NOT_FOUND
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-09 04:28:15

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72918729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档