4

When you want to share a library that uses a procedural macro, is the dual crate approach foo/foo_derive inevitable?

I would like to provide a crate that has the logic and the macros. The first thing I tried was:

my_proc_macro
├── Cargo.toml
├── src/lib.rs
└── my_crate
    ├── Cargo.toml
    └── src/lib.rs

In the my_proc_macro crate, I tried to pub use my_crate::*; but it is forbidden to do so: the compiler refused to build this.

Is it possible to do this the other way around? I mean: import the procedural macro crate into the library and then reexport the macro?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • 1
    I'm quite sure it can be done the other way around, but I'm not sure how. Look for example at [`rental`](https://crates.io/crates/rental), it has `rental-impl` as a dependency, but the user code does not have to write `extern crate rental_impl` so the `rental!` macro must be reexported by the main crate. – rodrigo Oct 22 '18 at 14:12
  • @rodrigo Unfortunately, I do not think that's what `rental` does: it exports [a "regular" macro](https://github.com/jpernst/rental/blob/master/src/lib.rs#L220) that does the derive – Boiethios Oct 22 '18 at 14:31

1 Answers1

6

It's actually quite straight-forward to re-export macros. Simply use

#[macro_use]
extern crate my_proc_macro;

in the root of my_crate.

The serde crate can be used this way when enabling the feature serde_derive.

In the 2018 edition, you can also explicitly re-export proc macros using use declarations.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841