Ho una macro che crea una struttura e un mucchio di funzioni di supporto e implementazioni di tratti. La cosa interessante per questa domanda è:Come si crea un tipo parametrizzato da una macro?
macro_rules! make_struct {
($name: ident) => {
struct $name;
}
}
Questo funziona come ci si aspetta:
make_struct!(MyStruct);
Se voglio fare un tipo parametrizzato però, io sono fuori di fortuna:
make_struct!(AnotherStruct<T: SomeTrait>);
test.rs:8:27: 8:28 error: no rules expected the token `<`
test.rs:8 make_struct!(AnotherStruct<T: SomeTrait>);
il nome del struct è un ident
quindi non può cambiare solo che nelle args macro (ad esempio per ty
):
test.rs:3:16: 3:21 error: expected ident, found `MyStruct`
test.rs:3 struct $name;
Quindi, come posso scrivere questa macro per poterla gestire entrambe? O ho bisogno di separare quelli? In quest'ultimo caso, che aspetto ha la macro?