diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-02-10 00:56:08 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-02-11 03:52:04 +0100 |
commit | 7680dcf3b86382baaf714306cb5bc8fea0e23ce4 (patch) | |
tree | ca7c9392e2e70a39d8ca3402dcf6f921388c1d77 | |
parent | b985c510aa3b00d560d4eb8e07fe210072c9343a (diff) |
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.
-rw-r--r-- | lib/etc/libimagutil/src/variants.rs | 24 |
1 files 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<A, B, C, F>(base: &A, modders: Vec<B>, f: &F) +pub fn generate_variants<A, B, C, F, I>(base: &A, modders: I, f: &F) -> Vec<C> where - F: Fn(&A, B) -> C + F: Fn(&A, B) -> C, + I: Iterator<Item = B> { - 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 |