Skip to content

Commit 6a6a04f

Browse files
committed
refactor(oma-fetch,oma-pm,oma-refresh,oma-topics)!: use async closure
1 parent 53571dc commit 6a6a04f

File tree

15 files changed

+86
-180
lines changed

15 files changed

+86
-180
lines changed

oma-fetch/examples/downloads.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async fn main() {
6464
.unwrap();
6565

6666
let summary = download_manager
67-
.start_download(|event| async {
67+
.start_download(async |event| {
6868
if let Err(e) = tx.send_async(event).await {
6969
eprintln!("Got Error: {:#?}", e);
7070
}

oma-fetch/src/download.rs

+18-39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{CompressFile, DownloadSource, Event, checksum::ChecksumValidator};
22
use std::{
33
fs::Permissions,
4-
future::Future,
54
io::{self, ErrorKind, SeekFrom},
65
os::unix::fs::PermissionsExt,
76
path::Path,
@@ -101,11 +100,7 @@ pub enum SingleDownloadError {
101100
}
102101

103102
impl SingleDownloader<'_> {
104-
pub(crate) async fn try_download<F, Fut>(self, callback: &F) -> DownloadResult
105-
where
106-
F: Fn(Event) -> Fut,
107-
Fut: Future<Output = ()>,
108-
{
103+
pub(crate) async fn try_download(self, callback: &impl AsyncFn(Event)) -> DownloadResult {
109104
let mut sources = self.entry.source.clone();
110105
assert!(!sources.is_empty());
111106

@@ -164,16 +159,12 @@ impl SingleDownloader<'_> {
164159
}
165160

166161
/// Download file with retry (http)
167-
async fn try_http_download<F, Fut>(
162+
async fn try_http_download(
168163
&self,
169164
source: &DownloadSource,
170165
auth: &Option<(String, String)>,
171-
callback: &F,
172-
) -> Result<bool, SingleDownloadError>
173-
where
174-
F: Fn(Event) -> Fut,
175-
Fut: Future<Output = ()>,
176-
{
166+
callback: &impl AsyncFn(Event),
167+
) -> Result<bool, SingleDownloadError> {
177168
let mut times = 1;
178169
let mut allow_resume = self.entry.allow_resume;
179170
loop {
@@ -210,17 +201,13 @@ impl SingleDownloader<'_> {
210201
}
211202
}
212203

213-
async fn http_download<F, Fut>(
204+
async fn http_download(
214205
&self,
215206
allow_resume: bool,
216207
source: &DownloadSource,
217208
auth: &Option<(String, String)>,
218-
callback: &F,
219-
) -> Result<bool, SingleDownloadError>
220-
where
221-
F: Fn(Event) -> Fut,
222-
Fut: Future<Output = ()>,
223-
{
209+
callback: &impl AsyncFn(Event),
210+
) -> Result<bool, SingleDownloadError> {
224211
let file = self.entry.dir.join(&*self.entry.filename);
225212
let file_exist = file.exists();
226213
let mut file_size = file.metadata().ok().map(|x| x.len()).unwrap_or(0);
@@ -588,16 +575,12 @@ impl SingleDownloader<'_> {
588575
}
589576

590577
/// Download local source file
591-
async fn download_local<F, Fut>(
578+
async fn download_local(
592579
&self,
593580
source: &DownloadSource,
594581
as_symlink: bool,
595-
callback: &F,
596-
) -> Result<bool, SingleDownloadError>
597-
where
598-
F: Fn(Event) -> Fut,
599-
Fut: Future<Output = ()>,
600-
{
582+
callback: &impl AsyncFn(Event),
583+
) -> Result<bool, SingleDownloadError> {
601584
debug!("{:?}", self.entry);
602585
let msg = self.progress_msg();
603586

@@ -701,16 +684,12 @@ impl SingleDownloader<'_> {
701684
Ok(true)
702685
}
703686

704-
async fn checksum_local<F, Fut>(
687+
async fn checksum_local(
705688
&self,
706-
callback: &F,
689+
callback: &impl AsyncFn(Event),
707690
url_path: &Path,
708691
hash: &crate::checksum::Checksum,
709-
) -> Result<(), SingleDownloadError>
710-
where
711-
F: Fn(Event) -> Fut,
712-
Fut: Future<Output = ()>,
713-
{
692+
) -> Result<(), SingleDownloadError> {
714693
let mut f = fs::File::open(url_path).await.context(OpenSnafu)?;
715694
let (size, finish) = checksum(callback, &mut f, &mut hash.get_validator()).await;
716695

@@ -724,11 +703,11 @@ impl SingleDownloader<'_> {
724703
}
725704
}
726705

727-
async fn checksum<F, Fut>(callback: &F, f: &mut File, v: &mut ChecksumValidator) -> (u64, bool)
728-
where
729-
F: Fn(Event) -> Fut,
730-
Fut: Future<Output = ()>,
731-
{
706+
async fn checksum(
707+
callback: &impl AsyncFn(Event),
708+
f: &mut File,
709+
v: &mut ChecksumValidator,
710+
) -> (u64, bool) {
732711
let mut reader = tokio::io::BufReader::with_capacity(READ_FILE_BUFSIZE, f);
733712

734713
let mut read = 0;

oma-fetch/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cmp::Ordering, path::PathBuf, time::Duration};
33
use bon::{Builder, builder};
44
use checksum::Checksum;
55
use download::{EmptySource, SingleDownloader, SuccessSummary};
6-
use futures::{Future, StreamExt};
6+
use futures::StreamExt;
77

88
use reqwest::{Client, Method, RequestBuilder};
99
use tracing::debug;
@@ -203,11 +203,10 @@ impl Summary {
203203

204204
impl DownloadManager<'_> {
205205
/// Start download
206-
pub async fn start_download<F, Fut>(&self, callback: F) -> Result<Summary, EmptySource>
207-
where
208-
F: Fn(Event) -> Fut,
209-
Fut: Future<Output = ()>,
210-
{
206+
pub async fn start_download(
207+
&self,
208+
callback: impl AsyncFn(Event),
209+
) -> Result<Summary, EmptySource> {
211210
let mut tasks = Vec::new();
212211
let mut list = vec![];
213212
for (i, c) in self.download_list.iter().enumerate() {

oma-pm/examples/install_fish.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn main() -> Result<(), OmaAptError> {
123123
network_thread: None,
124124
auth_config: Some(&AuthConfig::system("/").unwrap()),
125125
},
126-
|event| async {
126+
async |event| {
127127
if let Err(e) = tx.send_async(event).await {
128128
eprintln!("{:#?}", e);
129129
}

oma-pm/src/apt.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use ahash::HashSet;
1010
use apt_auth_config::AuthConfig;
1111
use bon::{Builder, builder};
1212
pub use oma_apt::cache::Upgrade;
13-
use std::future::Future;
1413
use tokio::runtime::Runtime;
1514
use zbus::Connection;
1615

@@ -418,16 +417,15 @@ impl OmaApt {
418417
}
419418

420419
/// Download packages
421-
pub fn download<F, Fut>(
420+
pub fn download<F>(
422421
&self,
423422
client: &Client,
424423
pkgs: Vec<OmaPackage>,
425424
config: DownloadConfig<'_>,
426425
callback: F,
427426
) -> OmaAptResult<Summary>
428427
where
429-
F: Fn(Event) -> Fut,
430-
Fut: Future<Output = ()>,
428+
F: AsyncFn(Event),
431429
{
432430
let mut download_list = vec![];
433431
for pkg in pkgs {
@@ -551,18 +549,14 @@ impl OmaApt {
551549
}
552550

553551
/// Commit changes
554-
pub fn commit<F, Fut>(
552+
pub fn commit(
555553
self,
556554
install_progress_manager: Box<dyn InstallProgressManager>,
557555
op: &OmaOperation,
558556
client: &Client,
559557
config: CommitNetworkConfig,
560-
callback: F,
561-
) -> OmaAptResult<()>
562-
where
563-
F: Fn(Event) -> Fut,
564-
Fut: Future<Output = ()>,
565-
{
558+
callback: impl AsyncFn(Event),
559+
) -> OmaAptResult<()> {
566560
let sysroot = self.config.get("Dir").unwrap_or("/".to_string());
567561

568562
if self.dry_run {

oma-pm/src/commit.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ impl<'a> DoInstall<'a> {
4747
})
4848
}
4949

50-
pub fn commit<F, Fut>(
50+
pub fn commit(
5151
self,
5252
op: &OmaOperation,
5353
install_progress_manager: Box<dyn InstallProgressManager>,
54-
callback: F,
55-
) -> OmaAptResult<()>
56-
where
57-
F: Fn(Event) -> Fut,
58-
Fut: std::future::Future<Output = ()>,
59-
{
54+
callback: impl AsyncFn(Event),
55+
) -> OmaAptResult<()> {
6056
let summary = self.download_pkgs(&op.install, callback)?;
6157

6258
if !summary.failed.is_empty() {
@@ -68,15 +64,11 @@ impl<'a> DoInstall<'a> {
6864
Ok(())
6965
}
7066

71-
fn download_pkgs<F, Fut>(
67+
fn download_pkgs(
7268
&self,
7369
download_pkg_list: &[InstallEntry],
74-
callback: F,
75-
) -> OmaAptResult<Summary>
76-
where
77-
F: Fn(Event) -> Fut,
78-
Fut: std::future::Future<Output = ()>,
79-
{
70+
callback: impl AsyncFn(Event),
71+
) -> OmaAptResult<Summary> {
8072
let path = self.apt.get_archive_dir();
8173
create_dir_all(path)
8274
.map_err(|e| OmaAptError::FailedOperateDirOrFile(path.display().to_string(), e))?;

oma-pm/src/download.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, future::Future, path::Path};
1+
use std::{borrow::Cow, path::Path};
22

33
use oma_console::console;
44
use oma_fetch::{
@@ -12,17 +12,13 @@ use tracing::debug;
1212
use crate::apt::{DownloadConfig, OmaAptError, OmaAptResult};
1313

1414
/// Download packages (inner)
15-
pub async fn download_pkgs<F, Fut>(
15+
pub async fn download_pkgs(
1616
client: &Client,
1717
download_pkg_list: &[InstallEntry],
1818
config: DownloadConfig<'_>,
1919
download_only: bool,
20-
callback: F,
21-
) -> OmaAptResult<Summary>
22-
where
23-
F: Fn(Event) -> Fut,
24-
Fut: Future<Output = ()>,
25-
{
20+
callback: impl AsyncFn(Event),
21+
) -> OmaAptResult<Summary> {
2622
let DownloadConfig {
2723
network_thread,
2824
download_dir,

oma-refresh/src/db.rs

+15-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::future::Future;
21
use std::{
32
borrow::Cow,
43
fs::Permissions,
@@ -192,11 +191,7 @@ pub enum Event {
192191
}
193192

194193
impl<'a> OmaRefresh<'a> {
195-
pub async fn start<F, Fut>(mut self, callback: F) -> Result<()>
196-
where
197-
F: Fn(Event) -> Fut,
198-
Fut: Future<Output = ()>,
199-
{
194+
pub async fn start(mut self, callback: impl AsyncFn(Event)) -> Result<()> {
200195
let arch = dpkg_arch(&self.source)?;
201196
let sourcelist = sources_lists(&self.source, &arch, &callback)
202197
.await
@@ -261,16 +256,12 @@ impl<'a> OmaRefresh<'a> {
261256
Ok(())
262257
}
263258

264-
async fn download_release_data<F, Fut>(
259+
async fn download_release_data(
265260
&self,
266-
callback: &F,
261+
callback: &impl AsyncFn(Event),
267262
tasks: &[DownloadEntry],
268263
total: u64,
269-
) -> Result<Summary>
270-
where
271-
F: Fn(Event) -> Fut,
272-
Fut: futures::Future,
273-
{
264+
) -> Result<Summary> {
274265
let dm = DownloadManager::builder()
275266
.client(self.client)
276267
.download_list(tasks)
@@ -330,16 +321,12 @@ impl<'a> OmaRefresh<'a> {
330321
}
331322
}
332323

333-
async fn download_releases<'b, F, Fut>(
324+
async fn download_releases<'b>(
334325
&mut self,
335326
sourcelist: &'b [OmaSourceEntry<'b>],
336327
replacer: &DatabaseFilenameReplacer,
337-
callback: &F,
338-
) -> Result<MirrorSources<'b, 'a>>
339-
where
340-
F: Fn(Event) -> Fut,
341-
Fut: Future<Output = ()>,
342-
{
328+
callback: &impl AsyncFn(Event),
329+
) -> Result<MirrorSources<'b, 'a>> {
343330
#[cfg(feature = "aosc")]
344331
let mut not_found = vec![];
345332

@@ -394,16 +381,12 @@ impl<'a> OmaRefresh<'a> {
394381
}
395382

396383
#[cfg(feature = "aosc")]
397-
async fn refresh_topics<'b, F, Fut>(
384+
async fn refresh_topics<'b>(
398385
&self,
399-
callback: &F,
386+
callback: &impl AsyncFn(Event),
400387
not_found: Vec<url::Url>,
401388
sources: &mut MirrorSources<'b, 'a>,
402-
) -> Result<()>
403-
where
404-
F: Fn(Event) -> Fut,
405-
Fut: Future<Output = ()>,
406-
{
389+
) -> Result<()> {
407390
if !self.refresh_topics || not_found.is_empty() {
408391
return Ok(());
409392
}
@@ -434,12 +417,8 @@ impl<'a> OmaRefresh<'a> {
434417
}
435418

436419
tm.write_enabled().await?;
437-
tm.write_sources_list(self.topic_msg, false, |topic, mirror| async move {
438-
callback(Event::TopicNotInMirror {
439-
topic: topic.to_string(),
440-
mirror: mirror.to_string(),
441-
})
442-
.await
420+
tm.write_sources_list(self.topic_msg, false, async move |topic, mirror| {
421+
callback(Event::TopicNotInMirror { topic, mirror }).await
443422
})
444423
.await?;
445424

@@ -449,16 +428,12 @@ impl<'a> OmaRefresh<'a> {
449428
}
450429

451430
#[cfg(not(feature = "aosc"))]
452-
async fn refresh_topics<'b, F, Fut>(
431+
async fn refresh_topics<'b>(
453432
&self,
454-
_callback: &F,
433+
_callback: &impl AsyncFn(Event),
455434
_not_found: Vec<url::Url>,
456435
_sources: &mut MirrorSources<'b, 'a>,
457-
) -> Result<()>
458-
where
459-
F: Fn(Event) -> Fut,
460-
Fut: Future<Output = ()>,
461-
{
436+
) -> Result<()> {
462437
Ok(())
463438
}
464439

0 commit comments

Comments
 (0)