I'm currently writing a procedural macro that can be used like this:
#[with_implementor(name = "impl_int_feature")]
/// An algebra containing the integer feature
pub trait IntAlg<T> {
/// Create an integeral literal
fn int(&self, i: isize) -> T;
}
This macro will create the trait and will create a macro named impl_int_feature
to easily implement this trait when using composition.
The following code can then be written to easily implement the above trait for a type using composition.
struct ComposedStruct {
int_helper: IntStruct, // IntStruct does implement IntAlg<T>
}
impl_int_feature!(ComposedStruct, int_helper, T);
This works fine. But now I would like to create a second procedural macro that can automatically create a struct and do the implementation:
language!(IntBoolLanguage<E> for IntAlg<E> as int_helper, BoolAlg<E> as bool_helper);
should create the following code:
struct IntBoolLanguage<E, T1: IntAlg<E>, T2: BoolAlg<E>> {
int_helper: T1,
bool_helper: T2
}
impl_int_feature!(IntBoolLanguage, int_helper, E);
impl_bool_feature!(IntBoolLanguage, bool_helper, E);
For this, I need to somehow get the macro_names (impl_int_feature and impl_bool_feature) from the passed traits (IntAlg, BoolAlg).
I currently do this, by keeping track of the macro_names in a HashMap within the proc_macro code. I add the name within the with_implementor
macro and read it in the language
macro. This works, but I need to make sure that all traits annotated with with_implementor
are compiled before the language
macros as the hashmap is otherwise not yet filled.
Is their an alternative way to keep track of this state without relying on the compilation order?