Reputation: 2147
Suppose I have the following code inside a module:
struct A{
}
#[macro_export]
macro_rules! profile {
($variable_name:tt, $expression:expr) => {
let a = A{};
}
}
so it'd be in a/a.rs
, to the side of a/mod.rs
which exports a::A
and also exports the macro.
The problem is that when I use this macro on other modules like b/b.rs
, I'd have to use super::a::A
before using the macro.
I could change let a = A{};
on the macro to let a = self::a::A{};
. However, it'd not work on all modules, and certainly not for users of this library that use this library in their code.
How can I specify let a = something::something::A{};
in a way that it works anywhere inside my library as well for users of this lib?
Upvotes: 4
Views: 437
Reputation: 5939
In declarative macros specifically, you can use the $crate
metavariable to reference the crate the macro is defined in, and then use the absolute path of the item. This will work both inside and outside of your library. For example, if your struct A
is defined in your library at the path module_a::module_b::A
, you would use:
struct A{
}
#[macro_export]
macro_rules! profile {
($variable_name:tt, $expression:expr) => {
let a = $crate::module_a::module_b::A{};
}
}
Here's the relevant section of the Rust reference guide.
Upvotes: 6