Change generate_variants() helper to use base by ref

This commit is contained in:
Matthias Beyer 2018-04-24 21:01:51 +02:00
parent 42c4186dec
commit 154c2e482b
2 changed files with 7 additions and 7 deletions

View file

@ -56,12 +56,12 @@ pub fn fetch_config(searchpath: &PathBuf) -> Result<Value> {
vec![ vec![
vec![searchpath.clone()], vec![searchpath.clone()],
gen_vars(searchpath.clone(), variants.clone(), &modifier), gen_vars(searchpath, variants.clone(), &modifier),
env::var("HOME").map(|home| gen_vars(PathBuf::from(home), variants.clone(), &modifier)) env::var("HOME").map(|home| gen_vars(&PathBuf::from(home), variants.clone(), &modifier))
.unwrap_or(vec![]), .unwrap_or(vec![]),
xdg_basedir::get_data_home().map(|data_dir| gen_vars(data_dir, variants.clone(), &modifier)) xdg_basedir::get_data_home().map(|data_dir| gen_vars(&data_dir, variants.clone(), &modifier))
.unwrap_or(vec![]), .unwrap_or(vec![]),
].iter() ].iter()
.flatten() .flatten()

View file

@ -32,12 +32,12 @@
* ``` * ```
* *
*/ */
pub fn generate_variants<A, B, C, F>(base: A, modders: Vec<B>, f: &F) pub fn generate_variants<A, B, C, F>(base: &A, modders: Vec<B>, f: &F)
-> Vec<C> -> Vec<C>
where where
F: Fn(&A, B) -> C F: Fn(&A, B) -> C
{ {
modders.into_iter().map(|m| f(&base, m)).collect() modders.into_iter().map(|m| f(base, m)).collect()
} }
#[cfg(test)] #[cfg(test)]
@ -49,7 +49,7 @@ mod test {
fn test_variants_simple() { fn test_variants_simple() {
let base = 1; let base = 1;
let vars = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let vars = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let res = generate_variants(base, vars, &|base, var| base + var); let res = generate_variants(&base, vars, &|base, var| base + var);
assert!(res.len() == 11, format!("Length is {} instead of 11", res.len())); assert!(res.len() == 11, format!("Length is {} instead of 11", res.len()));
assert!(res.iter().all(|i| *i > 0)); assert!(res.iter().all(|i| *i > 0));
@ -62,7 +62,7 @@ mod test {
let base = PathBuf::from("/"); let base = PathBuf::from("/");
let vars = vec!["foo", "bar", "baz"]; let vars = vec!["foo", "bar", "baz"];
let res = generate_variants(base, vars, &|base, var| { let res = generate_variants(&base, vars, &|base, var| {
let mut base = base.clone(); let mut base = base.clone();
base.push(var); base.push(var);
base base