I am trying to implement a rust macro with items. Both Osc
and Multiply
implement a trait with const id: [char; 3]
and fn new(cctx: &CreationalContext) -> Self
The code is:
macro_rules! ifid {
(($id:expr, $ctx:expr) $($node:item),+) => {
{
$(
if ($id == $node::id) {
return $node::new($ctx)
}
)+
}
}
}
pub fn id_to_node(id: [char; 3], context: CreationalContext) -> impl Process {
ifid! {
(id, context)
osc::Osc,
multiply::Multiply,
}
}
and the error is:
expected one of `!` or `::`, found `,`
expected one of `!` or `::`rustc
If I remove the commas from the macro, a similar error occurs.
P.S. I know you probably can't return
from a macro; I tried to assign the new struct instance to a variable, but variables cannot implement traits: let it: &impl Process;
errors.
Context:
I want to match a three char id sent from js to a new instance of a struct that implies a trait called Process
. I tried putting all the constructors in a vector, but you can only use impl in a function return type, not in the return type of a pointer to a function.