如何导出同名的函数和宏?

How to export function and macro with the same name?

是否可以从模块中导出同名的函数和宏?

示例lib.rs

mod log;

fn foo() {
    log::info!("");
    log::info("");
}

log.rs中:

宏和函数属于不同的namespaces so two with the same name should happily coexist. This compiles (playground):

macro_rules! info {
    ($v:expr) => {}
}

fn info(v: &str) { }

但是,当试图从一个模块中 public 生成它们时似乎会出现问题。导出宏,如 How do I use a macro across module files? seems to conflict somehow with the function declaration (playground):

所示
mod log {
    macro_rules! info {
        ($v:expr) => {}
    }
    
    pub(crate) use info;
    
    pub fn info(v: &str) { }
}
error[E0255]: the name `info` is defined multiple times
 --> src/lib.rs:8:5
  |
6 |     pub(crate) use info;
  |                    ---- previous import of the value `info` here
7 |     
8 |     pub fn info(v: &str) { }
  |     ^^^^^^^^^^^^^^^^^^^^ `info` redefined here
  |
  = note: `info` must be defined only once in the value namespace of this module
help: you can use `as` to change the binding name of the import
  |
6 |     pub(crate) use info as other_info;
  |                    ~~~~~~~~~~~~~~~~~~

我不知道这是错误还是有意为之。无论哪种方式都令人困惑。


我发现的解决方法是在单独的模块中声明该函数,然后在原始模块 (playground) 中通过通配符重新导出它:

mod log {
    mod imp {
        pub fn info(v: &str) { }
    }
    
    pub use imp::*;
    
    macro_rules! info {
        ($v:expr) => { }
    }
    
    pub(crate) use info;
}

你可以用另一种方式来做(即将宏放在一个单独的模块中),但编译器奇怪地产生了一个警告,它没有重新导出任何东西,即使它做了 (playground)。