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.
This commit is contained in:
parent
b985c510aa
commit
7680dcf3b8
1 changed files with 11 additions and 13 deletions
|
@ -23,22 +23,20 @@
|
||||||
/// Example:
|
/// Example:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let base = "Base";
|
/// use libimagutil::variants::generate_variants;
|
||||||
/// let variants = generate_variants(base, vec!["foo", "bar", "baz"], |base, modder| {
|
/// let base = 1;
|
||||||
/// let mut variant = base.clone();
|
/// let vars = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||||
/// variant.push(modder);
|
/// let res = generate_variants(&base, vars.iter(), &|base, var| base + var);
|
||||||
/// variant
|
/// assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
|
||||||
/// });
|
|
||||||
///
|
|
||||||
/// assert!(variants, vec!["Basefoo", "Basebar", "Basebaz"]);
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
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>
|
-> Vec<C>
|
||||||
where
|
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)]
|
#[cfg(test)]
|
||||||
|
@ -50,7 +48,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.iter(), &|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));
|
||||||
|
@ -63,7 +61,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.iter(), &|base, var| {
|
||||||
let mut base = base.clone();
|
let mut base = base.clone();
|
||||||
base.push(var);
|
base.push(var);
|
||||||
base
|
base
|
||||||
|
|
Loading…
Reference in a new issue