From 7680dcf3b86382baaf714306cb5bc8fea0e23ce4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 10 Feb 2019 00:56:08 +0100 Subject: [PATCH] Make generate_variants() more flexible This patch changes generate_variants() so that it allows to be passed an iterator for the modders, which makes it more powerful for passing references. --- lib/etc/libimagutil/src/variants.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/etc/libimagutil/src/variants.rs b/lib/etc/libimagutil/src/variants.rs index 9775caf5..544201d8 100644 --- a/lib/etc/libimagutil/src/variants.rs +++ b/lib/etc/libimagutil/src/variants.rs @@ -23,22 +23,20 @@ /// Example: /// /// ``` -/// let base = "Base"; -/// let variants = generate_variants(base, vec!["foo", "bar", "baz"], |base, modder| { -/// let mut variant = base.clone(); -/// variant.push(modder); -/// variant -/// }); -/// -/// assert!(variants, vec!["Basefoo", "Basebar", "Basebaz"]); +/// use libimagutil::variants::generate_variants; +/// let base = 1; +/// let vars = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +/// let res = generate_variants(&base, vars.iter(), &|base, var| base + var); +/// assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) /// ``` /// -pub fn generate_variants(base: &A, modders: Vec, f: &F) +pub fn generate_variants(base: &A, modders: I, f: &F) -> Vec where - F: Fn(&A, B) -> C + F: Fn(&A, B) -> C, + I: Iterator { - modders.into_iter().map(|m| f(base, m)).collect() + modders.map(|m| f(base, m)).collect() } #[cfg(test)] @@ -50,7 +48,7 @@ mod test { fn test_variants_simple() { let base = 1; 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.iter(), &|base, var| base + var); assert!(res.len() == 11, format!("Length is {} instead of 11", res.len())); assert!(res.iter().all(|i| *i > 0)); @@ -63,7 +61,7 @@ mod test { let base = PathBuf::from("/"); let vars = vec!["foo", "bar", "baz"]; - let res = generate_variants(&base, vars, &|base, var| { + let res = generate_variants(&base, vars.iter(), &|base, var| { let mut base = base.clone(); base.push(var); base