Last active
August 31, 2018 14:30
-
-
Save yaymukund/66130719d1d1ec1b105d799eac58fa2c 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::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 = ()> { | |
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
// type mismatch resolving `<[closure@src/downloader/download.rs:30:36: 35:10 sender:_] as | |
// std::ops::FnOnce<(hyper::Response<hyper::Body>,)>>::Output == ()`: expected struct | |
// `futures::sink::SendAll`, found () | |
let parsed_uri: Uri = self.uri.parse().unwrap(); | |
let (sender, receiver) = mpsc::channel(5); | |
self.body = Some(receiver); | |
client.get(parsed_uri).map(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!())) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment