Skip to content

Commit

Permalink
Add macro gen_many_byref
Browse files Browse the repository at this point in the history
Iterating on containers can be for in three different ways: using &v,
ref v, or v. Currently we cannot guess which notation to use, thus
provide three macros.
This solution is unsatisfying, before we have a better one.
  • Loading branch information
chifflier committed Jun 2, 2017
1 parent f1a4741 commit 822020d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/gen.rs
Expand Up @@ -822,6 +822,23 @@ macro_rules! gen_many_ref(
);
);

/// `gen_many_byref!(I, Iterable<V>, Fn(I,V)) => I -> Result<I,E>`
/// Applies the generator `$f` to every element of `$l`, where arguments of $l are references
#[macro_export]
macro_rules! gen_many_byref(
(($i:expr, $idx:expr), $l:expr, $f:expr) => (
$l.into_iter().fold(
Ok(($i,$idx)),
|r,&v| {
match r {
Err(e) => Err(e),
Ok(x) => { $f(x, v) },
}
}
)
);
);

/// `gen_many!(I, Iterable<V>, Fn(I,V)) => I -> Result<I,E>`
/// Applies the generator `$f` to every element of `$l`, passing arguments by value.
#[macro_export]
Expand Down

0 comments on commit 822020d

Please sign in to comment.