Hyper具有功能fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>
可將HTTP響應的內容讀取到提供的&mut [u8]
中。使用Hyper和Flate2讀取gzip響應
Flate2可以用gunzip:
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
}
(迴應刪除的評論...)是的,我做了[嘗試&zs']。結果'trait'&& mut [u8]:std :: io :: Read'not satisfied'。 – Synesso
你可以嘗試添加第二個&它嗎? (這聽起來很愚蠢,但它適用於[playground](https://play.rust-lang.org/?code=use%20std%3A%3Aio%3A%3ARead%3B%0A%0Afn%20foo(r %3A%20%26Read)%20%7B%0A%20%20%20%20%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut% 20A%20%3D%20%5B8u8%2C%206%2C%207%5D%3B%0A%20%20%20%20let%20mut%20bind%20%3D%20%26A%3B%0A%20% 20%20%20%0%20%20%20%20foo(%26%26bind%5B ..%5D)%3B%0A%7D&version = stable&backtrace = 0)) – Neikos
'GzDecoder :: new(&& zs).unwrap ()'=>'trait'&&& mut [u8]:std :: io :: Read'not satisfied'。 – Synesso