diff --git a/src/dist/download.rs b/src/dist/download.rs index 12824548d53..60f1f3af677 100644 --- a/src/dist/download.rs +++ b/src/dist/download.rs @@ -1,6 +1,7 @@ use std::fs; use std::ops; use std::path::{Path, PathBuf}; +use std::sync::Arc; use anyhow::{anyhow, Context, Result}; use sha2::{Digest, Sha256}; @@ -13,12 +14,12 @@ use crate::utils::utils; const UPDATE_HASH_LEN: usize = 20; -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct DownloadCfg<'a> { pub dist_root: &'a str, pub temp_cfg: &'a temp::Cfg, pub download_dir: &'a PathBuf, - pub notify_handler: &'a dyn Fn(Notification<'_>), + pub notify_handler: Arc)>, } pub(crate) struct File { @@ -76,7 +77,7 @@ impl<'a> DownloadCfg<'a> { &partial_file_path, Some(&mut hasher), true, - &|n| (self.notify_handler)(n.into()), + Some(Arc::new(|n| (self.notify_handler)(n.into()))), ) .await { @@ -170,7 +171,7 @@ impl<'a> DownloadCfg<'a> { let file = self.temp_cfg.new_file_with_ext("", ext)?; let mut hasher = Sha256::new(); - utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), &|n| { + utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), |n| { (self.notify_handler)(n.into()) }))?; let actual_hash = format!("{:x}", hasher.finalize()); diff --git a/src/dist/manifestation/tests.rs b/src/dist/manifestation/tests.rs index 6eb2f361362..ab4809c8fa2 100644 --- a/src/dist/manifestation/tests.rs +++ b/src/dist/manifestation/tests.rs @@ -447,7 +447,7 @@ fn update_from_dist( &manifest_url, &manifest_file, None, - &|_| {}, + None, ))?; let manifest_str = utils::read_file("manifest", &manifest_file)?; let manifest = Manifest::parse(&manifest_str)?; diff --git a/src/utils/utils.rs b/src/utils/utils.rs index d55124600cc..d6489191764 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -3,6 +3,7 @@ use std::fs::{self, File}; use std::future::Future; use std::io::{self, BufReader, Write}; use std::path::{Path, PathBuf}; +use std::sync::Arc; use anyhow::{anyhow, bail, Context, Result}; use home::env as home; @@ -138,9 +139,9 @@ pub async fn download_file( url: &Url, path: &Path, hasher: Option<&mut Sha256>, - notify_handler: &dyn Fn(Notification<'_>), + notify_handler: Option)>>, ) -> Result<()> { - download_file_with_resume(url, path, hasher, false, ¬ify_handler).await + download_file_with_resume(url, path, hasher, false, notify_handler).await } pub(crate) async fn download_file_with_resume( @@ -148,7 +149,7 @@ pub(crate) async fn download_file_with_resume( path: &Path, hasher: Option<&mut Sha256>, resume_from_partial: bool, - notify_handler: &dyn Fn(Notification<'_>), + notify_handler: Option)>>, ) -> Result<()> { use download::DownloadError as DEK; match download_file_(url, path, hasher, resume_from_partial, notify_handler).await { @@ -183,14 +184,16 @@ async fn download_file_( path: &Path, hasher: Option<&mut Sha256>, resume_from_partial: bool, - notify_handler: &dyn Fn(Notification<'_>), + notify_handler: Option)>>, ) -> Result<()> { use download::download_to_path_with_backend; use download::{Backend, Event, TlsBackend}; use sha2::Digest; use std::cell::RefCell; - notify_handler(Notification::DownloadingFile(url, path)); + notify_handler + .as_ref() + .map(|notify_handler| notify_handler(Notification::DownloadingFile(url, path))); let hasher = RefCell::new(hasher); @@ -203,7 +206,7 @@ async fn download_file_( } } - match msg { + notify_handler.as_ref().map(|notify_handler| match msg { Event::DownloadContentLengthReceived(len) => { notify_handler(Notification::DownloadContentLengthReceived(len)); } @@ -213,7 +216,7 @@ async fn download_file_( Event::ResumingPartialDownload => { notify_handler(Notification::ResumingPartialDownload); } - } + }); Ok(()) }; @@ -240,12 +243,14 @@ async fn download_file_( }; (Backend::Reqwest(tls_backend), Notification::UsingReqwest) }; - notify_handler(notification); + notify_handler + .as_ref() + .map(|notify_handler| notify_handler(notification)); let res = download_to_path_with_backend(backend, url, path, resume_from_partial, Some(callback)) .await; - notify_handler(Notification::DownloadFinished); + notify_handler.map(|notify_handler| notify_handler(Notification::DownloadFinished)); res }