How to make macro import/use struct in any place it's used?

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?

1 Like

$crate inside a macro always refers to the crate where the macro is defined, so

let a = $crate::a::A;

should do it as long as the module and type are both public.

1 Like