Skip to main content


I implemented a #RustLang derive macro that generates code which uses functionality from the serde_derive crate. Drawback: the crate using this derive macro must have serde_derive imported toplevel, and it must not be aliased. How do I properly decouple from that? Is it possible to re-export the dependency in my derive-macro crate? How would I resolve my crate in an absolute manner? It appears $crate is not available in derive macros. #followerpower
Did you try ::serde_derive? With the double ":" before it
You can only export macros from a proc macro crate. You need a wrapper crate that reexports the macro and serde_derive. Let say you name it macro_helper. This you could use ::macro_helper::serde_derive. Did not try it, just out of my head.
@mo8it Thanks for the hint, I'll investigate how this works.
@mo8it Worked nicely. Now the only restriction is that the crate which re-exports the macros must not be aliased, but I assume I won't get around that restriction, right? #RustLang
I think that aliasing is not a problem as long as you start every path with "::". But it might be a case I am not aware of. Could you explain where it fails on aliasing?
@mo8it Let's say I call my user-facing wrapper crate "myderive" and the macro crate "myderive_impl". A user could then import "myderive" under a different name, and all references in "myderive_impl" to "::myderive" would fail there.
@mo8it In glib-macros/etc we're using the proc-macro-crate crate for this purpose. It's not perfect but catches the most common cases.
@slomo @mo8it Wow, that's great! I'm delighted and at the same time not really surprised somebody solved this issue in a generic and reusable manner. Will try that crate.
Serde itself has this problem, and addresses it via an attribute that the derive can consume, e.g.:

#[derive(Serialize)]
#[serde(crate = "some_alias")]
struct Foo { ... }

https://serde.rs/container-attrs.html#crate
@latk Nice, thanks a lot! Although not as convenient as could be, it's an interesting pragmatic workaround for that problem.