Created
August 26, 2018 01:32
-
-
Save yaymukund/0f810098390e50f9e5e7ea39dfd8d377 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::client::ResponseFuture; | |
use hyper::rt::Future; | |
use hyper::Uri; | |
enum DownloadStatus { | |
Idle, | |
PendingResponse, | |
PendingBody, | |
Completed, | |
Errored, | |
} | |
pub struct Download { | |
uri: String, | |
status: DownloadStatus, | |
future: ResponseFuture, | |
} | |
impl Download { | |
pub fn new(uri: &str, client: &HttpClient) -> Download { | |
let parsed_uri: Uri = uri.parse().unwrap(); | |
Download { | |
uri: uri.to_owned(), | |
status: DownloadStatus::Idle, | |
future: client.get(parsed_uri), | |
} | |
} | |
pub fn future(&mut self) -> impl Future { | |
^^^^^^^^^^^ lifetime of return value does not outlive the function call | |
self.status = DownloadStatus::PendingResponse; | |
self.future.map(|res| { | |
self.status = if res.status().is_success() { | |
DownloadStatus::PendingResponse | |
} else { | |
DownloadStatus::Errored | |
}; | |
res.body() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment