I'm working on a proc macro that generates macro_rules macros as part of its output. I need these macros to be accessible outside of the crate they're generated in. However, I know you can't do this:
mod mod_a {
#[macro_export]
macro_rules! foo {
() => {}
}
}
mod mod_b {
#[macro_export]
macro_rules! foo {
() => {}
}
}
My proc macro may be used in multiple different places in the same crate, and I'd like to allow that to happen if it's done in different submodules. I'm a little stuck on how to do that while still getting exportable macros.
One thing I could do is:
mod mod_a {
#[macro_export]
macro_rules! foo_1 {
() => {}
}
pub use foo_1 as foo;
}
mod mod_b {
#[macro_export]
macro_rules! foo_2 {
() => {}
}
pub use foo_2 as foo;
}
And then mod_a::foo
and mod_b::foo
would be usable. However, this requires the proc macro to assign a unique name to each macro. I could possibly do this by hashing the file path and row-column info from the Span::call_site()
, but unfortunately that's unstable functionality (proc_macro_span
). I could generate a UUID, but that would make it difficult to cache the macro output since it would be constantly changing the output irrespective of changes to the input.
Is there another way of accomplishing this goal of generating multiple macros with the same name, exported from the crate, but distinguished by their path?