rust - 我可以在 Rocket 中使用异步 fn 作为处理程序吗?
问题描述
我正在使用 Rocket 框架,我想在我的处理程序中发出一个异步 HTTP 请求,就像这样
#[get("/")]
async fn handler() -> String {
some_func().await;
"OK".into()
}
结果,我得到了下一个错误
the trait `rocket::response::Responder<'_>` is not implemented for `impl core::future::future::Future`
我试图编写实现但失败了。有没有办法为 impl Trait 实现 trait?
或者也许指定 async fn 的返回类型,以便我可以返回具有必要特征的自定义类型?
解决方案
在 Rocket 0.5.0 发布之前,您必须将开发版本用于异步路由。值得注意的是,这也意味着您可以使用稳定的 Rust 进行编译。
在你的 Cargo.toml
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket" }
使用开发版本后,您可以完全按照您在问题中所做的那样编写异步路由。
请注意,各种 API 可能不同。有关开发版本的文档,请参阅https://api.rocket.rs/master/rocket/index.html。
推荐阅读
- php - 有没有办法将整个 url 作为变量传递给 onclick=window.open?
- java - tIECategory 表存在,但一直落入“其他”部分......任何代码错误或其他?
- android - 在 AndroidGradlePlugin 3.5.0 及更高版本上为动态功能模块使用片段测试库时构建失败
- cloudkit - Node JS 上的 CloudKit 服务器到服务器身份验证出现 401 错误
- node.js - 使用 ZIP 将 nodejs 应用程序部署到 Elastic BeanStalk => 找不到本地包
- java - Spring:如何配置 Spring bean 定义以在传递相同参数时返回相同的 bean 实例
- javascript - 如何在 MkDocs 中将脚本添加到 head 标签?
- python-3.x - 我如何在 python 提示符 linux 上获取数据帧执行的时间
- r - 对于具有连续和分类预测变量的 glmer 模型,应该使用什么事后检验?
- python - python为什么如果为零,当零= 0时打印('True'),它什么也不打印?