Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import/export macro

Tags:

macros

rust

I have a module named macros.rs which 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.rs I import the module like this

#[macro_use] mod macros;

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

like image 393
SBSTP Avatar asked Jan 08 '23 16:01

SBSTP


1 Answers

Turns out that the order in which you declare 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's ok this way.
like image 144
SBSTP Avatar answered Jan 13 '23 10:01

SBSTP