Skip to content

Commit db70182

Browse files
lj3954flexiondotorg
authored andcommitted
refactor: Add macro to join futures
1 parent 5e5c3ad commit db70182

File tree

17 files changed

+106
-202
lines changed

17 files changed

+106
-202
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
/join_futures/target

Cargo.lock

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ serde = "1.0.202"
2020
serde_json = "1.0.117"
2121
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "sync"] }
2222
zstd = "0.13.1"
23+
join_futures = { path = "join_futures" }

join_futures/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

join_futures/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "join_futures"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[profile.dev]
7+
opt-level = 3
8+
9+
[lib]
10+
proc-macro = true

join_futures/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use proc_macro::{TokenStream, TokenTree};
2+
3+
#[proc_macro]
4+
pub fn join_futures(input: TokenStream) -> TokenStream {
5+
let mut tokens = input.into_iter();
6+
7+
let variable = match tokens.find(|t| matches!(t, TokenTree::Ident(_))) {
8+
Some(TokenTree::Ident(variable)) => variable.to_string(),
9+
_ => panic!("You must provide a variable containing futures to join"),
10+
};
11+
let flatten_amount = match tokens.find(|t| matches!(t, TokenTree::Literal(_))) {
12+
Some(TokenTree::Literal(flatten_amount)) => flatten_amount.to_string().trim().parse::<usize>().unwrap(),
13+
_ => 0,
14+
};
15+
16+
let tokens = tokens.skip_while(|t| matches!(t, TokenTree::Punct(_)));
17+
let value_type = match tokens.map(|t| t.to_string()).collect::<String>() {
18+
s if s.is_empty() => "Vec<Config>".to_string(),
19+
s => s,
20+
};
21+
22+
let mut value = format!("futures::future::join_all({variable}).await");
23+
if flatten_amount > 0 {
24+
value.push_str(&format!(
25+
".into_iter(){}.collect::<{value_type}>()",
26+
".flatten()".repeat(flatten_amount),
27+
));
28+
}
29+
value.parse().unwrap()
30+
}

src/bsd/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::store_data::{ArchiveFormat, ChecksumSeparation, Config, Disk, Distro, Source, WebSource};
22
use crate::utils::capture_page;
3+
use join_futures::join_futures;
34
use quickemu::config::{Arch, GuestOS};
45
use regex::Regex;
56
use std::sync::Arc;
@@ -84,22 +85,14 @@ impl Distro for FreeBSD {
8485
[normal_editions, vm_image]
8586
})
8687
.collect::<Vec<_>>();
87-
Some(futures::future::join_all(futures).await)
88+
Some(join_futures!(futures))
8889
} else {
8990
log::warn!("Failed to fetch FreeBSD {arch} releases");
9091
None
9192
}
9293
}
9394
});
94-
futures::future::join_all(futures)
95-
.await
96-
.into_iter()
97-
.flatten()
98-
.flatten()
99-
.flatten()
100-
.flatten()
101-
.collect::<Vec<Config>>()
102-
.into()
95+
Some(join_futures!(futures, 4))
10396
}
10497
}
10598

@@ -205,16 +198,10 @@ impl Distro for GhostBSD {
205198
}
206199
})
207200
.collect::<Vec<_>>();
208-
Some(futures::future::join_all(futures).await)
201+
Some(join_futures!(futures))
209202
}
210203
});
211204

212-
futures::future::join_all(futures)
213-
.await
214-
.into_iter()
215-
.flatten()
216-
.flatten()
217-
.collect::<Vec<Config>>()
218-
.into()
205+
Some(join_futures!(futures, 2))
219206
}
220207
}

src/linux/arch.rs

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
store_data::{ChecksumSeparation, Config, Distro, Source, WebSource},
55
utils::{capture_page, GatherData, GithubAPI},
66
};
7+
use join_futures::join_futures;
78
use regex::Regex;
89
use serde::Deserialize;
910
use std::sync::Arc;
@@ -39,12 +40,7 @@ impl Distro for Archcraft {
3940
})
4041
}
4142
});
42-
futures::future::join_all(futures)
43-
.await
44-
.into_iter()
45-
.flatten()
46-
.collect::<Vec<Config>>()
47-
.into()
43+
Some(join_futures!(futures, 1))
4844
}
4945
}
5046

@@ -147,17 +143,11 @@ impl Distro for ArcoLinux {
147143
}
148144
})
149145
.collect::<Vec<_>>();
150-
Some(futures::future::join_all(futures).await)
146+
Some(join_futures!(futures))
151147
}
152148
})
153149
.collect::<Vec<_>>();
154-
futures::future::join_all(futures)
155-
.await
156-
.into_iter()
157-
.flatten()
158-
.flatten()
159-
.collect::<Vec<Config>>()
160-
.into()
150+
Some(join_futures!(futures, 2))
161151
}
162152
}
163153

@@ -235,12 +225,7 @@ impl Distro for AthenaOS {
235225
})
236226
});
237227

238-
futures::future::join_all(futures)
239-
.await
240-
.into_iter()
241-
.flatten()
242-
.collect::<Vec<Config>>()
243-
.into()
228+
Some(join_futures!(futures, 1))
244229
}
245230
}
246231

@@ -302,17 +287,11 @@ impl Distro for CachyOS {
302287
})
303288
.collect::<Vec<_>>();
304289

305-
Some(futures::future::join_all(futures).await)
290+
Some(join_futures!(futures))
306291
}
307292
});
308293

309-
futures::future::join_all(futures)
310-
.await
311-
.into_iter()
312-
.flatten()
313-
.flatten()
314-
.collect::<Vec<Config>>()
315-
.into()
294+
Some(join_futures!(futures, 2))
316295
}
317296
}
318297

@@ -341,7 +320,7 @@ impl Distro for EndeavourOS {
341320
}
342321
}
343322
});
344-
futures::future::join_all(futures).await.into()
323+
Some(join_futures!(futures))
345324
}
346325
}
347326

@@ -383,11 +362,6 @@ impl Distro for Garuda {
383362
}
384363
});
385364

386-
futures::future::join_all(futures)
387-
.await
388-
.into_iter()
389-
.flatten()
390-
.collect::<Vec<Config>>()
391-
.into()
365+
Some(join_futures!(futures, 1))
392366
}
393367
}

src/linux/arch/manjaro.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
store_data::{Config, Distro, Source, WebSource},
33
utils::capture_page,
44
};
5+
use join_futures::join_futures;
56
use regex::Regex;
67

78
const BIGLINUX_MIRROR: &str = "https://iso.biglinux.com.br/";
@@ -38,6 +39,6 @@ impl Distro for BigLinux {
3839
}
3940
});
4041

41-
futures::future::join_all(futures).await.into()
42+
Some(join_futures!(futures))
4243
}
4344
}

src/linux/debian.rs

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
store_data::{ChecksumSeparation, Config, Disk, Distro, Source, WebSource},
33
utils::{capture_page, GatherData, GithubAPI},
44
};
5+
use join_futures::join_futures;
56
use quickemu::config::{Arch, DiskFormat};
67
use quickget_core::data_structures::ArchiveFormat;
78
use regex::Regex;
@@ -68,13 +69,7 @@ impl Distro for Antix {
6869
}
6970
});
7071

71-
futures::future::join_all(futures)
72-
.await
73-
.into_iter()
74-
.flatten()
75-
.flatten()
76-
.collect::<Vec<Config>>()
77-
.into()
72+
Some(join_futures!(futures, 2))
7873
}
7974
}
8075

@@ -96,12 +91,7 @@ impl Distro for BunsenLabs {
9691
let url = format!("{BUNSENLABS_MIRROR}{}", &c[1]);
9792
async move { ChecksumSeparation::Whitespace.build(&url).await }
9893
});
99-
let mut checksums = futures::future::join_all(checksum_futures)
100-
.await
101-
.into_iter()
102-
.flatten()
103-
.flatten()
104-
.collect::<HashMap<String, String>>();
94+
let mut checksums = join_futures!(checksum_futures, 2, HashMap<String, String>);
10595

10696
release_regex
10797
.captures_iter(&html)
@@ -254,14 +244,7 @@ impl Distro for Debian {
254244
})
255245
.flatten();
256246

257-
futures::future::join_all(futures)
258-
.await
259-
.into_iter()
260-
.flatten()
261-
.flatten()
262-
.flatten()
263-
.collect::<Vec<Config>>()
264-
.into()
247+
Some(join_futures!(futures, 3))
265248
}
266249
}
267250

@@ -310,13 +293,7 @@ impl Distro for Devuan {
310293
)
311294
}
312295
});
313-
futures::future::join_all(futures)
314-
.await
315-
.into_iter()
316-
.flatten()
317-
.flatten()
318-
.collect::<Vec<Config>>()
319-
.into()
296+
Some(join_futures!(futures, 2))
320297
}
321298
}
322299

@@ -360,22 +337,10 @@ impl Distro for EasyOS {
360337
}
361338
});
362339

363-
Some(
364-
futures::future::join_all(futures)
365-
.await
366-
.into_iter()
367-
.flatten()
368-
.collect::<Vec<_>>(),
369-
)
340+
Some(join_futures!(futures))
370341
}
371342
});
372-
let mut releases = futures::future::join_all(release_futures)
373-
.await
374-
.into_iter()
375-
.flatten()
376-
.flatten()
377-
.flatten()
378-
.collect::<Vec<_>>();
343+
let mut releases = join_futures!(release_futures, 4, Vec<(String, String)>);
379344

380345
releases.sort_by(|(a, _), (b, _)| {
381346
if let (Ok(a), Ok(b)) = (
@@ -425,12 +390,7 @@ impl Distro for EasyOS {
425390
})
426391
}
427392
});
428-
futures::future::join_all(futures)
429-
.await
430-
.into_iter()
431-
.flatten()
432-
.collect::<Vec<Config>>()
433-
.into()
393+
Some(join_futures!(futures, 1))
434394
}
435395
}
436396

@@ -478,17 +438,10 @@ impl Distro for EndlessOS {
478438
})
479439
}
480440
});
481-
Some(futures::future::join_all(futures).await)
441+
Some(join_futures!(futures))
482442
}
483443
});
484444

485-
futures::future::join_all(futures)
486-
.await
487-
.into_iter()
488-
.flatten()
489-
.flatten()
490-
.flatten()
491-
.collect::<Vec<_>>()
492-
.into()
445+
Some(join_futures!(futures, 3))
493446
}
494447
}

0 commit comments

Comments
 (0)