Skip to content

Commit 3c4ced6

Browse files
committed
WIP
1 parent 49ebf74 commit 3c4ced6

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎src/dist/download.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fs;
22
use std::ops;
33
use std::path::{Path, PathBuf};
4+
use std::sync::Arc;
45

56
use anyhow::{anyhow, Context, Result};
67
use sha2::{Digest, Sha256};
@@ -13,12 +14,12 @@ use crate::utils::utils;
1314

1415
const UPDATE_HASH_LEN: usize = 20;
1516

16-
#[derive(Copy, Clone)]
17+
#[derive(Clone)]
1718
pub struct DownloadCfg<'a> {
1819
pub dist_root: &'a str,
1920
pub temp_cfg: &'a temp::Cfg,
2021
pub download_dir: &'a PathBuf,
21-
pub notify_handler: &'a dyn Fn(Notification<'_>),
22+
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
2223
}
2324

2425
pub(crate) struct File {
@@ -76,7 +77,7 @@ impl<'a> DownloadCfg<'a> {
7677
&partial_file_path,
7778
Some(&mut hasher),
7879
true,
79-
&|n| (self.notify_handler)(n.into()),
80+
Some(Arc::new(|n| (self.notify_handler)(n.into()))),
8081
)
8182
.await
8283
{
@@ -170,7 +171,7 @@ impl<'a> DownloadCfg<'a> {
170171
let file = self.temp_cfg.new_file_with_ext("", ext)?;
171172

172173
let mut hasher = Sha256::new();
173-
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), &|n| {
174+
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), |n| {
174175
(self.notify_handler)(n.into())
175176
}))?;
176177
let actual_hash = format!("{:x}", hasher.finalize());

‎src/dist/manifestation/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ fn update_from_dist(
447447
&manifest_url,
448448
&manifest_file,
449449
None,
450-
&|_| {},
450+
None,
451451
))?;
452452
let manifest_str = utils::read_file("manifest", &manifest_file)?;
453453
let manifest = Manifest::parse(&manifest_str)?;

‎src/utils/utils.rs‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::{self, File};
33
use std::future::Future;
44
use std::io::{self, BufReader, Write};
55
use std::path::{Path, PathBuf};
6+
use std::sync::Arc;
67

78
use anyhow::{anyhow, bail, Context, Result};
89
use 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

146147
pub(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

Comments
 (0)