summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@sequoia-pgp.org>2022-04-10 11:32:49 +0300
committerLars Wirzenius <liw@sequoia-pgp.org>2022-04-10 11:32:49 +0300
commit60661ed168a2424c0adfaec4e798b4239a53db7f (patch)
tree742c98ca940e2c892249c2a7b5e036162288b524
parent4092f57f0768c54efc351e5fe22f7fe34f70edb4 (diff)
downloadchecksums-60661ed168a2424c0adfaec4e798b4239a53db7f.tar.gz
refactor: modules for clarity
Sponsored-by: author
-rw-r--r--src/main.rs84
1 files changed, 49 insertions, 35 deletions
diff --git a/src/main.rs b/src/main.rs
index b775127..37babe5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,62 +1,76 @@
use checksums::*;
use clap::Parser;
-use rayon::prelude::*;
-use std::path::PathBuf;
-use std::thread::{spawn, JoinHandle};
fn main() {
let args = Args::parse();
- if let Err(e) = checksums_linear(&args.filenames) {
+ if let Err(e) = linear::checksums(&args.filenames) {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
println!();
- if let Err(e) = checksums_rayon(&args.filenames) {
+ if let Err(e) = rayon::checksums(&args.filenames) {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
println!();
- if let Err(e) = checksums_threads(&args.filenames) {
+ if let Err(e) = threads::checksums(&args.filenames) {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
}
-fn checksums_linear(filenames: &[PathBuf]) -> anyhow::Result<()> {
- println!("linear");
- let checksums: Result<Vec<FileChecksum>, std::io::Error> = filenames
- .iter()
- .map(|filename| checksum(filename))
- .collect();
- print_checksums(&checksums?);
- Ok(())
+mod linear {
+ use checksums::*;
+ use std::path::PathBuf;
+
+ pub fn checksums(filenames: &[PathBuf]) -> anyhow::Result<()> {
+ println!("linear");
+ let checksums: Result<Vec<FileChecksum>, std::io::Error> = filenames
+ .iter()
+ .map(|filename| checksum(filename))
+ .collect();
+ print_checksums(&checksums?);
+ Ok(())
+ }
}
-fn checksums_rayon(filenames: &[PathBuf]) -> anyhow::Result<()> {
- println!("rayon");
- let checksums: Result<Vec<FileChecksum>, std::io::Error> = filenames
- .par_iter()
- .map(|filename| checksum(filename))
- .collect();
- print_checksums(&checksums?);
- Ok(())
+mod rayon {
+ use checksums::*;
+ use rayon::prelude::*;
+ use std::path::PathBuf;
+
+ pub fn checksums(filenames: &[PathBuf]) -> anyhow::Result<()> {
+ println!("rayon");
+ let checksums: Result<Vec<FileChecksum>, std::io::Error> = filenames
+ .par_iter()
+ .map(|filename| checksum(filename))
+ .collect();
+ print_checksums(&checksums?);
+ Ok(())
+ }
}
-fn checksums_threads(filenames: &[PathBuf]) -> anyhow::Result<()> {
- println!("threads");
- let handles: Vec<JoinHandle<Result<FileChecksum, std::io::Error>>> = filenames
- .iter()
- .cloned()
- .map(|filename| spawn(move || checksum(&filename.clone())))
- .collect();
- let mut checksums = vec![];
- for handle in handles {
- let result = handle.join().expect("thread join");
- checksums.push(result?);
+mod threads {
+ use checksums::*;
+ use std::path::PathBuf;
+ use std::thread::{spawn, JoinHandle};
+
+ pub fn checksums(filenames: &[PathBuf]) -> anyhow::Result<()> {
+ println!("threads");
+ let handles: Vec<JoinHandle<Result<FileChecksum, std::io::Error>>> = filenames
+ .iter()
+ .cloned()
+ .map(|filename| spawn(move || checksum(&filename)))
+ .collect();
+ let mut checksums = vec![];
+ for handle in handles {
+ let result = handle.join().expect("thread join");
+ checksums.push(result?);
+ }
+ print_checksums(&checksums);
+ Ok(())
}
- print_checksums(&checksums);
- Ok(())
}