Skip to content

Instantly share code, notes, and snippets.

@yaymukund
Created August 27, 2018 18:02
Show Gist options
  • Save yaymukund/a032151ae1b18f2cf2870724fa5cbf1c to your computer and use it in GitHub Desktop.
Save yaymukund/a032151ae1b18f2cf2870724fa5cbf1c to your computer and use it in GitHub Desktop.
use super::downloader::HttpClient;
use hyper::rt::{Future, Stream};
use hyper::Uri;
use std::sync::{Arc, Mutex};
pub struct Download {
uri: String,
data: Arc<Mutex<Vec<u8>>>,
}
impl Download {
pub fn new(uri: &str) -> Download {
Download {
uri: uri.to_owned(),
data: Arc::new(Mutex::new(Vec::new())),
}
}
// Why is this future so lazy?
pub fn make_future(&mut self, client: &HttpClient) -> impl Future<Item = (), Error = ()> {
let parsed_uri: Uri = self.uri.parse().unwrap();
let data = self.data.clone();
client
.get(parsed_uri)
.map(move |res| {
res.into_body().for_each(move |chunk| {
let mut data = data.lock().unwrap();
data.extend(chunk);
println!("processing chunk");
// ^^
Ok(())
})
})
.map(|_| ())
.map_err(|_| ())
}
pub fn display_progress(&self) {
println!("{}: {} bytes", self.uri, self.data.lock().unwrap().len());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment