diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index 044ee8c9..927948b0 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -524,6 +524,39 @@ impl Dow } } +/// Async join support for file downloader +pub struct DownloaderJoinHandle< + T: Write + Seek + Send + Sync + ClearFile + GetTargetFileName + SetLen, +> { + /// internal type + downloader: Arc>, +} + +impl + From>> for DownloaderJoinHandle +{ + fn from(value: Arc>) -> Self { + Self { downloader: value } + } +} + +impl std::future::Future + for DownloaderJoinHandle +{ + type Output = Result<(), DownloaderError>; + fn poll( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll { + if self.downloader.is_downloading() { + cx.waker().wake_by_ref(); + std::task::Poll::Pending + } else { + std::task::Poll::Ready(Ok(())) + } + } +} + /// A file downloader /// /// When dropping downloader, the downloader need some times to let all threads exited. @@ -618,14 +651,8 @@ impl Result<(), DownloaderError> { - loop { - if !self.is_downloading() { - break; - } - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - Ok(()) + pub fn join(&self) -> DownloaderJoinHandle { + DownloaderJoinHandle::from(self.downloader.clone()) } #[inline]