Rust 使用超板条箱主体作为未来板条箱流参数

Rust 使用超板条箱主体作为未来板条箱流参数,rust,hyper,Rust,Hyper,使用,我向端点发出HTTP请求,然后尝试将响应主体传递给第三方库,该库希望参数为 这将导致类型错误 Cargo.toml [dependencies] bytes = "1.0.1" http = "0.2.3" tokio = { version = "1.1.0", features = ["full"] } hyper = { version = "0.14.2", features = [

使用,我向端点发出HTTP请求,然后尝试将响应主体传递给第三方库,该库希望参数为

这将导致类型错误

Cargo.toml

[dependencies]
bytes = "1.0.1"
http = "0.2.3"
tokio = { version = "1.1.0", features = ["full"] }
hyper = { version = "0.14.2", features = ["full"] }
hyper-tls = "0.5.0"
futures = "0.3.12"
示例

使用std::io;
使用字节::字节;
使用hyper::{Client,Body};
使用hyper_tls::HttpsConnector;
使用http::请求;
使用futures::stream::stream;
//----开始第三方库
类型ConsumableStream=dyn Stream+Send+Sync+'static;
异步fn流\u使用者(\u:&mut ConsumableStream){
//消费流。。。
}
//----结束第三方库
#[tokio::main]
异步fn main(){
让uri=”https://jsonplaceholder.typicode.com/todos/1";
让https=HttpsConnector::new();
让client=client::builder().build::(https);
let request=request::get(uri).body(body::empty()).unwrap();
让response=client.request(request.wait.unwrap();
让mut body=Box::new(response.into_body());
流_消费者(&mut body)。等待;
}
货物运行输出

error[E0271]:类型不匹配解析`::Item==std::result::result`
-->src/bin/future_streams.rs:24:21
|
24 |流|消费者(和多体)。等待;
|^^^^^^^^^预期结构为'std::io::Error',找到结构为'hyper::Error::Error'`
|
=注意:预期枚举`std::result::result`
找到枚举'std::result::result'`
=注意:强制转换到对象类型`(dyn\u core::stream::stream+std::marker::Send+std::marker::Sync+'static)需要`
错误:由于以前的错误而中止;1发出警告
有关此错误的详细信息,请尝试“rustc--explain E0271”。
错误:无法编译“rustest”。
要了解更多信息,请使用--verbose再次运行该命令。
问题


使用hyper Body with作为预期未来流类型的函数参数最有效的方法是什么?

ConsumableStream
预期的是
结果,但
客户端。请求
返回
结果
。如果
ConsumableStream
来自第三方库,并且您无法更改类型定义,则可以映射结果流:

use futures::trystreamText;
使用std::io;
#[tokio::main]
异步fn main(){
// ...
让身体=反应
.进入_body()
.map_err(| e | io::Error::new(io::ErrorKind::Other,e));
stream_consumer(&mut Box::new(body))。等待;
}

Ahhh,好的!因此,如果出现错误结果,
.map\u err
函数将允许您重新定义错误。这个很好用,谢谢!