@@ -3,6 +3,7 @@ use std::fs::{self, File};
3
3
use std:: future:: Future ;
4
4
use std:: io:: { self , BufReader , Write } ;
5
5
use std:: path:: { Path , PathBuf } ;
6
+ use std:: sync:: Arc ;
6
7
7
8
use anyhow:: { anyhow, bail, Context , Result } ;
8
9
use home:: env as home;
@@ -138,17 +139,17 @@ pub async fn download_file(
138
139
url : & Url ,
139
140
path : & Path ,
140
141
hasher : Option < & mut Sha256 > ,
141
- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
142
+ notify_handler : Option < Arc < & dyn Fn ( Notification < ' _ > ) > > ,
142
143
) -> 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
144
145
}
145
146
146
147
pub ( crate ) async fn download_file_with_resume (
147
148
url : & Url ,
148
149
path : & Path ,
149
150
hasher : Option < & mut Sha256 > ,
150
151
resume_from_partial : bool ,
151
- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
152
+ notify_handler : Option < Arc < & dyn Fn ( Notification < ' _ > ) > > ,
152
153
) -> Result < ( ) > {
153
154
use download:: DownloadError as DEK ;
154
155
match download_file_ ( url, path, hasher, resume_from_partial, notify_handler) . await {
@@ -183,14 +184,16 @@ async fn download_file_(
183
184
path : & Path ,
184
185
hasher : Option < & mut Sha256 > ,
185
186
resume_from_partial : bool ,
186
- notify_handler : & dyn Fn ( Notification < ' _ > ) ,
187
+ notify_handler : Option < Arc < & dyn Fn ( Notification < ' _ > ) > > ,
187
188
) -> Result < ( ) > {
188
189
use download:: download_to_path_with_backend;
189
190
use download:: { Backend , Event , TlsBackend } ;
190
191
use sha2:: Digest ;
191
192
use std:: cell:: RefCell ;
192
193
193
- notify_handler ( Notification :: DownloadingFile ( url, path) ) ;
194
+ notify_handler
195
+ . as_ref ( )
196
+ . map ( |notify_handler| notify_handler ( Notification :: DownloadingFile ( url, path) ) ) ;
194
197
195
198
let hasher = RefCell :: new ( hasher) ;
196
199
@@ -203,7 +206,7 @@ async fn download_file_(
203
206
}
204
207
}
205
208
206
- match msg {
209
+ notify_handler . as_ref ( ) . map ( |notify_handler| match msg {
207
210
Event :: DownloadContentLengthReceived ( len) => {
208
211
notify_handler ( Notification :: DownloadContentLengthReceived ( len) ) ;
209
212
}
@@ -213,7 +216,7 @@ async fn download_file_(
213
216
Event :: ResumingPartialDownload => {
214
217
notify_handler ( Notification :: ResumingPartialDownload ) ;
215
218
}
216
- }
219
+ } ) ;
217
220
218
221
Ok ( ( ) )
219
222
} ;
@@ -240,12 +243,14 @@ async fn download_file_(
240
243
} ;
241
244
( Backend :: Reqwest ( tls_backend) , Notification :: UsingReqwest )
242
245
} ;
243
- notify_handler ( notification) ;
246
+ notify_handler
247
+ . as_ref ( )
248
+ . map ( |notify_handler| notify_handler ( notification) ) ;
244
249
let res =
245
250
download_to_path_with_backend ( backend, url, path, resume_from_partial, Some ( callback) )
246
251
. await ;
247
252
248
- notify_handler ( Notification :: DownloadFinished ) ;
253
+ notify_handler. map ( |notify_handler| notify_handler ( Notification :: DownloadFinished ) ) ;
249
254
250
255
res
251
256
}
0 commit comments