@@ -3,6 +3,7 @@ use std::fs::{self, File};
33use std:: future:: Future ;
44use std:: io:: { self , BufReader , Write } ;
55use std:: path:: { Path , PathBuf } ;
6+ use std:: sync:: Arc ;
67
78use anyhow:: { anyhow, bail, Context , Result } ;
89use home:: env as home;
@@ -138,17 +139,17 @@ pub async fn download_file(
138139 url : & Url ,
139140 path : & Path ,
140141 hasher : Option < & mut Sha256 > ,
141- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
142+ notify_handler : Option < Arc < dyn Fn ( Notification < ' _ > ) > > ,
142143) -> Result < ( ) > {
143- download_file_with_resume ( url, path, hasher, false , & notify_handler) . await
144+ download_file_with_resume ( url, path, hasher, false , notify_handler) . await
144145}
145146
146147pub ( crate ) async fn download_file_with_resume (
147148 url : & Url ,
148149 path : & Path ,
149150 hasher : Option < & mut Sha256 > ,
150151 resume_from_partial : bool ,
151- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
152+ notify_handler : Option < Arc < dyn Fn ( Notification < ' _ > ) > > ,
152153) -> Result < ( ) > {
153154 use download:: DownloadError as DEK ;
154155 match download_file_ ( url, path, hasher, resume_from_partial, notify_handler) . await {
@@ -183,14 +184,16 @@ async fn download_file_(
183184 path : & Path ,
184185 hasher : Option < & mut Sha256 > ,
185186 resume_from_partial : bool ,
186- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
187+ notify_handler : Option < Arc < dyn Fn ( Notification < ' _ > ) > > ,
187188) -> Result < ( ) > {
188189 use download:: download_to_path_with_backend;
189190 use download:: { Backend , Event , TlsBackend } ;
190191 use sha2:: Digest ;
191192 use std:: cell:: RefCell ;
192193
193- notify_handler ( Notification :: DownloadingFile ( url, path) ) ;
194+ notify_handler
195+ . as_ref ( )
196+ . map ( |notify_handler| notify_handler ( Notification :: DownloadingFile ( url, path) ) ) ;
194197
195198 let hasher = RefCell :: new ( hasher) ;
196199
@@ -203,7 +206,7 @@ async fn download_file_(
203206 }
204207 }
205208
206- match msg {
209+ notify_handler . as_ref ( ) . map ( |notify_handler| match msg {
207210 Event :: DownloadContentLengthReceived ( len) => {
208211 notify_handler ( Notification :: DownloadContentLengthReceived ( len) ) ;
209212 }
@@ -213,7 +216,7 @@ async fn download_file_(
213216 Event :: ResumingPartialDownload => {
214217 notify_handler ( Notification :: ResumingPartialDownload ) ;
215218 }
216- }
219+ } ) ;
217220
218221 Ok ( ( ) )
219222 } ;
@@ -240,12 +243,14 @@ async fn download_file_(
240243 } ;
241244 ( Backend :: Reqwest ( tls_backend) , Notification :: UsingReqwest )
242245 } ;
243- notify_handler ( notification) ;
246+ notify_handler
247+ . as_ref ( )
248+ . map ( |notify_handler| notify_handler ( notification) ) ;
244249 let res =
245250 download_to_path_with_backend ( backend, url, path, resume_from_partial, Some ( callback) )
246251 . await ;
247252
248- notify_handler ( Notification :: DownloadFinished ) ;
253+ notify_handler. map ( |notify_handler| notify_handler ( Notification :: DownloadFinished ) ) ;
249254
250255 res
251256}
0 commit comments