【发布时间】:2020-04-06 21:20:13
【问题描述】:
我正在尝试将来自 reqwest 的流转换为 actix 响应的流,以避免在内存中创建大的 Vec:
use actix_web::{web, HttpRequest, HttpResponse, Responder};
pub async fn test(
req: HttpRequest
) -> impl Responder {
let request = reqwest::get("https://www.rust-lang.org").await;
if request.is_err() {
println!("Error: {:?}", request.err());
}
let request = request.unwrap();
let stream = request.bytes_stream();
HttpResponse::Ok()
.content_type("text/html")
.streaming(stream)
}
编译器给了我:
--> src/routes/test.rs:97:10
|
97 | .streaming(stream)
| ^^^^^^^^^ the trait `actix_http::error::ResponseError` is not implemented for `reqwest::error::Error`
|
= note: required because of the requirements on the impl of `std::convert::From<reqwest::error::Error>` for `actix_http::error::Error`
= note: required because of the requirements on the impl of `std::convert::Into<actix_http::error::Error>` for `reqwest::error::Error`
我已经尽力了,但没有运气:
- 创建一个传递
Stream(连编译都不行,我不擅长转换类型) - 实现 Reqwest 错误的缺失错误(该错误未公开,因此我无法这样做)
- 无论如何都在尝试(但不确定我是否使用正确)
【问题讨论】:
-
很难回答您的问题,因为它不包含minimal reproducible example。我们无法分辨代码中存在哪些 crate(及其版本)、类型、特征、字段等。如果您尝试在Rust Playground 上重现您的错误,如果可能的话,这将使我们更容易为您提供帮助,否则在全新的 Cargo 项目中,然后在edit 您的问题中包含附加信息。您可以使用Rust-specific MRE tips 来减少您在此处发布的原始代码。谢谢!
-
Rust proper error handling (auto convert from one error type to another with question mark)的答案可能会回答您的问题。如果没有,请edit您的问题来解释差异。否则,我们可以将此问题标记为已回答。
-
看看两个答案,谢谢:)
标签: rust rust-tokio