Is there any way to export both declarative macros and procedural macros from a single crate?

I've created a simple crate, that exports convenient macros for decreasing the amount of duplicate code, it would be nice to export both declarative macros and procedural macros from this crate. I couldn't find the answer on the internet so I came here to ask if it's possible or is there a way around that restriction: error: cannot export macro_rules! macros from a proc-macro crate type currently

There's not really any way to get around the fact that proc macros have to be defined in their special own crate type, but a way to accomplish the same effect could be a public facing crate that defines your declarative macros and re-exports the proc-macros.

1 Like

I thought about what you said and it really helped me, thank you.

how to do?

I've basically created an additional crate for the main one, which contains all of the procedural macros, and re-exported it in the main crate like this: pub extern crate your_proc_macros_crate;. And after that you can use main_crate::your_proc_macros_crate::something. Here's an example:

├── Cargo.toml
├── proc-macros-crate
│   ├── Cargo.toml
│   ├── README.md
│   └── src
│       ├── ...
│       └── lib.rs
├── src
│   ├── ...
│   └── lib.rs
└── tests
    ├── Cargo.lock
    ├── Cargo.toml
    └── lib.rs

in tests/lib.rs:
#[cfg(test)]
mod tests {
    ...
    use main_crate::*;
    use main_crate::proc_macros_crate::*;
    ...

Or you can see how it looks like in my noobie repo

You can also reexport each macro individually with pub use proc_macro_crate::macro_name;, which can be nice for downstream users so they don't need to know about proc_macro_crate.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.