Created
August 27, 2018 16:04
-
-
Save yaymukund/64e91b365ea0ba950e99ab623d9bc74b 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; | |
pub struct Download { | |
uri: String, | |
data: Arc<Vec<u8>>, | |
} | |
impl Download { | |
pub fn new(uri: &str) -> Download { | |
Download { | |
uri: uri.to_owned(), | |
data: Arc::new(Vec::new()), | |
} | |
} | |
pub fn make_future<'a>( | |
&'a mut self, | |
client: &HttpClient, | |
) -> impl Future<Item = (), Error = ()> + 'a { | |
let parsed_uri: Uri = self.uri.parse().unwrap(); | |
let data = Arc::make_mut(&mut self.data); | |
client | |
.get(parsed_uri) | |
.map(|res| { | |
// ^^^^ | |
// closure may outlive the current function, but it borrows | |
// `data`, which is owned by the current function: may outlive | |
// borrowed value `data` | |
res.into_body().and_then(|chunk| { | |
data.extend(chunk); | |
// ^^ | |
Ok(()) | |
}); | |
}) | |
.map_err(|_| ()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment