Created
August 27, 2018 18:02
-
-
Save yaymukund/a032151ae1b18f2cf2870724fa5cbf1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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