src/client/http_client.rs - download: use generic Write type, return writer.

Make it possible to write int Vec<u8>.
This commit is contained in:
Dietmar Maurer 2019-06-26 09:17:13 +02:00
parent bb8231409e
commit c2b945341c

View File

@ -7,6 +7,7 @@ use xdg::BaseDirectories;
use chrono::Utc; use chrono::Utc;
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::io::Write;
use http::{Request, Response}; use http::{Request, Response};
use http::header::HeaderValue; use http::header::HeaderValue;
@ -208,7 +209,7 @@ impl HttpClient {
self.request(req) self.request(req)
} }
pub fn download(&mut self, path: &str, mut output: Box<dyn std::io::Write + Send>) -> impl Future<Item=(), Error=Error> { pub fn download<W: Write>(&mut self, path: &str, output: W) -> impl Future<Item=W, Error=Error> {
let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap(); let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap();
@ -234,9 +235,9 @@ impl HttpClient {
future::Either::B( future::Either::B(
resp.into_body() resp.into_body()
.map_err(Error::from) .map_err(Error::from)
.for_each(move |chunk| { .fold(output, move |mut acc, chunk| {
output.write_all(&chunk)?; acc.write_all(&chunk)?;
Ok(()) Ok::<_, Error>(acc)
}) })
) )
} }