Created
August 31, 2018 15:13
-
-
Save yaymukund/ff7f28902de4e6e4ca97824dc498e8b8 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 futures::future; | |
use futures::sink::Sink; | |
use futures::sync::mpsc; | |
use hyper::rt::{Future, Stream}; | |
use hyper::{Chunk, Uri}; | |
use std::sync::{atomic, Arc}; | |
pub struct Download { | |
uri: String, | |
body: Option<mpsc::Receiver<Chunk>>, | |
len: Arc<atomic::AtomicUsize>, | |
} | |
impl Download { | |
pub fn new(uri: &str) -> Download { | |
Download { | |
uri: uri.to_owned(), | |
body: None, | |
len: Arc::new(atomic::AtomicUsize::new(0)), | |
} | |
} | |
pub fn make_future(&mut self, client: &HttpClient) -> impl Future<Item = (), Error = ()> { | |
let parsed_uri: Uri = self.uri.parse().unwrap(); | |
let (sender, receiver) = mpsc::channel(5); | |
self.body = Some(receiver); | |
client.get(parsed_uri).map_err(|_| ()).and_then(move |res| { | |
let body = res.into_body(); | |
sender | |
// got this from https://github.com/rust-lang-nursery/futures-rs/issues/401 | |
// but it's copypasta, and i don't want to leave it in if i can help it. | |
.send_all(body.map_err(|_| unreachable!())) | |
.map(|_| ()) | |
.map_err(|_| ()) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment