Skip to content

Commit b9911b8

Browse files
downloader: Implement Future trait for DownloadHandle
This is the idomatic way
1 parent 9adb239 commit b9911b8

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/downloader.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ pub struct DownloadHandle {
3434
cancel: oneshot::Sender<()>,
3535
}
3636

37-
impl DownloadHandle {
38-
pub async fn r#await(self) -> Result<File, Error> {
39-
match self.result.await {
40-
Ok(result) => result,
41-
Err(_) => todo!(),
37+
impl std::future::Future for DownloadHandle {
38+
type Output = Result<tokio::fs::File, Error>;
39+
40+
fn poll(
41+
mut self: std::pin::Pin<&mut Self>,
42+
cx: &mut std::task::Context<'_>,
43+
) -> std::task::Poll<Self::Output> {
44+
use std::pin::Pin;
45+
use std::task::Poll;
46+
match Pin::new(&mut self.result).poll(cx) {
47+
Poll::Ready(Ok(result)) => Poll::Ready(result),
48+
Poll::Ready(Err(e)) => Poll::Ready(Err(Error::Oneshot(e))),
49+
Poll::Pending => Poll::Pending,
4250
}
4351
}
52+
}
4453

54+
impl DownloadHandle {
4555
pub fn status(&self) -> Status {
4656
self.status.borrow().clone()
4757
}

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ pub enum Error {
88
Serde(#[from] serde_json::Error),
99
#[error("Reqwest: {0}")]
1010
Reqwest(#[from] reqwest::Error),
11+
#[error("Oneshot: {0}")]
12+
Oneshot(#[from] tokio::sync::oneshot::error::RecvError),
1113
}

0 commit comments

Comments
 (0)