I have a struct A and a macro test_factory that provides syntactic sugar for creating test instances of A:
mod a {
pub struct A {}
#[macro_export]
macro_rules! test_factory { () -> crate::a::A{} }
}
#[cfg(test)]
mod b {
#[test]
fn test_something() {
let my_a = crate::test_factory!();
//...
}
}
The project grew, and I am turning it into a workspace with several packages. I want the test_factory! macro to be usable for tests in all the workspace's packages, so I separated it out into a separate package, and add it to the other packages' dev-dependencies.
When I use the macro from the same package where A is defined, it works. If I use it from another package, the compiler fails saying that there is no crate::a::A, which is of course correct. If I change the macro so it references A by crate:
But I moved to macro to a separate crate, so I can add it to dev-dependencies of other packages and have it available in their tests.
As I understand $crate, it refers to the crate the macro is defined in, and not the crate a struct like A is defined in. Am I missing something?
Ah, I misunderstood, sorry. If I am not again mistaken, one work-around would be to define either more macro match arms or multiple macros, and pass a path substitution variable around.
I found a solution that works even better in my context: I re-export A in the shared crate using pub use A, and then reference it in the macro using $crate.