Failed to import / export macro

I have a module with a name macros.rsthat contains

/// Macro to easily implement FromError.
/// from_error!(MyError, IoError, MyError::IoError)
macro_rules! from_error {
    ( $t:ty, $err:ty, $name:path ) => {
        use std;
        impl std::error::FromError<$err> for $t {
            fn from_error(err: $err) -> $t {
                $name(err)
            }
        }
    }
}

In my main.rsI import the module as follows

#[macro_use] mod macros;

When I try to use from_errorin other modules of my project, the compiler says error: macro undefined: 'from_error!'.

+4
source share
1 answer

It turns out that the order in which you declare the modules matters.

mod json; // json uses macros from the "macros" module. can't find them.
#[macro_use]
mod macros;

#[macro_use]
mod macros;
mod json; // json uses macros from the "macros" module. everything ok this way.
+8
source

All Articles