From be6a0b8b78f636baaa47489bb502904672fa8329 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 17 Jan 2016 20:08:27 +0100 Subject: [PATCH] util: Add variant-generator utility --- libimagutil/src/lib.rs | 4 +--- libimagutil/src/variants.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 libimagutil/src/variants.rs diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index a93251b6..0797cbab 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1,3 +1 @@ -#[test] -fn it_works() { -} +pub mod variants; diff --git a/libimagutil/src/variants.rs b/libimagutil/src/variants.rs new file mode 100644 index 00000000..0ec53830 --- /dev/null +++ b/libimagutil/src/variants.rs @@ -0,0 +1,23 @@ +/** + * Generate variants of a base value by applying parts + * + * Example: + * + * ```ignore + * generate_variants(path, vec!["foo", "bar", "baz"], |b, v| { + * let b = b.clone(); + * b.push(v); + * b + * }) + * + * ``` + * + */ +pub fn generate_variants(base: A, modders: Vec, f: &F) + -> Vec + where + F: Fn(&A, B) -> C +{ + modders.into_iter().map(|m| f(&base, m)).collect() +} +