From 14e3725fa93b4c786b27a63c0a95cc313fc89668 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 24 Nov 2020 19:18:54 +0000 Subject: [PATCH] working debug rel --- fcmprs/Cargo.toml | 8 ++++- fcmprs/src/main.rs | 82 +++++++++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/fcmprs/Cargo.toml b/fcmprs/Cargo.toml index 6f06b50..40d43fc 100644 --- a/fcmprs/Cargo.toml +++ b/fcmprs/Cargo.toml @@ -6,7 +6,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["threads"] + +threads = ["rayon"] + [dependencies] +cfg-if = "1.0.0" memmap = "0.7.0" -rayon = "1.5.0" +rayon = {version = "1.5.0", optional = true} smallvec = "1.5.0" diff --git a/fcmprs/src/main.rs b/fcmprs/src/main.rs index 4292a95..9318d9b 100644 --- a/fcmprs/src/main.rs +++ b/fcmprs/src/main.rs @@ -1,9 +1,10 @@ -use rayon::prelude::*; +#[cfg(feature="threads")] use rayon::prelude::*; use std::{ path::Path, io, fs::{self, OpenOptions,}, }; use smallvec::SmallVec; +use cfg_if::cfg_if; fn usage() -> ! { @@ -18,6 +19,8 @@ use error::ResultPrintExt as _; mod map; +struct UnmatchError; + fn main() { let (map1, rest) = { let mut args = std::env::args().skip(1); @@ -28,34 +31,59 @@ fn main() { } }; - std::process::exit(dbg!(if let Some(map1) = map::map(&map1).discard_msg(format!("Failed to map file {}", map1)) { - let slice = map1.as_slice(); - let mut ok = true; - let chk: SmallVec<[_; 32]> = rest.filter_map(|filename| { - let path = Path::new(&filename); - if path.exists() && path.is_file() { - map::map(path).discard_msg(format!("Failed to map file {}", filename)) - .or_else(|| { ok = false; None }) + std::process::exit(dbg!{ + if let Some(map1) = map::map(&map1).discard_msg(format!("Failed to map file {}", map1)) { + let slice = map1.as_slice(); + let mut ok = true; + let chk: SmallVec<[_; 32]> = rest.filter_map(|filename| { + let path = Path::new(&filename); + if path.exists() && path.is_file() { + map::map(path).discard_msg(format!("Failed to map file {}", filename)) + .or_else(|| { ok = false; None }) + } else { + eprintln!("File {} does not exist or is not a normal file", filename); + ok = false; + None + } + }).collect(); + + if !ok { + -1 } else { - eprintln!("File {} does not exist or is not a normal file", filename); - ok = false; - None - } - }).collect(); - if !ok { - -1 - } else { - match chk.into_par_iter() - .map(|map| slice == map.as_slice()) - .reduce_with(|x, y| x && y) - { - Some(true) => 0, - Some(false) => 1, - None => usage(), + cfg_if! { + if #[cfg(feature="threads")] { + match chk.into_par_iter() + .map(|map| { + if slice == map.as_slice() { + Ok(()) + }else{ + Err(UnmatchError) + } + }) + .try_reduce_with(|_, _| Ok(())) + { + Some(Ok(_)) => 0, + Some(Err(_)) => 1, + None => usage(), + } + } else { + match chk.into_iter() + .map(|map| { + slice == map.as_slice() + }) + .try_fold((false, true), |(_, a), b| if a && b {Ok((true, true))} else {Err(UnmatchError)}) + { + Ok((true, _)) => 0, + Ok((false, _)) => usage(), + Err(_) => 1, + } + + } + } } + } else { + -1 } - } else { - -1 - })) + }) }