Hyper 具有将fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>
HTTP 响应的内容读取到提供的&mut [u8]
.
Flate2 可以压缩:
let mut d = GzDecoder::new("...".as_bytes()).unwrap();
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
println!("{}", s);
我试着把这两件事放在一起:
fn gunzip(r: &Response) -> String {
let mut zs: &mut [u8] = &mut[];
r.read(zs);
let mut d = GzDecoder::new(zs).unwrap();
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
s
}
我得到了错误:
error[E0277]: the trait bound `[u8]: std::io::Read` is not satisfied
--> tests/integration.rs:232:21
|
232 | let mut d = GzDecoder::new(zs).unwrap();
| ^^^^^^^^^^^^^^ trait `[u8]: std::io::Read` not satisfied
|
= help: the following implementations were found:
= help: <&'a [u8] as std::io::Read>
= note: required because of the requirements on the impl of `std::io::Read` for `&mut [u8]`
= note: required by `<flate2::read::DecoderReader<R>>::new`
我哪里错了?
编辑:最终的工作解决方案:
fn gunzip(r: &mut Response) -> String {
let mut buffer = Vec::new();
let _ = r.read_to_end(&mut buffer).unwrap();
let mut d = GzDecoder::new(buffer.as_slice()).unwrap();
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
s
}