What I want to solve is
lookup!(cfg, val => [A, B, C, D, ... Z]);
So example:
let c: C = lookup!("blarf", 2 => [A, B, C, D, ...Z]) // Equivalent of calling C::create("blarf");
All types implement trait
trait Create {
create(cfg: ...) -> Self;
}
The macro definition would look something like:
macro_rules! lookup {
($cfg:expr, $m:expr => [ $( $a:ty ),* ]) => {
match $m {
$(
i => <$a>::create(cfg)
)*
}
}
}
But I cannot figure out how the macro would be for the match statement. Any suggestions?
P.S.
I could of course just write a function as:
let i = 2;
let cfg = ...
let c: C = match i {
0 => A::create(cfg);
1 => B::create(cfg);
2 => C::create(cfg);
...
};