# Re-exporting a trait with a custom derive\_macro

**URL:** <https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767>\
**Category:** help\
**Created:** [April 24, 2026, 1:20pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767 "2026-04-24T13:20:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KayJay7](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kayjay7/32/52281_2.png) [@KayJay7](https://users.rust-lang.org/u/KayJay7)\
**Post date:** [April 24, 2026, 1:20pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767/1 "2026-04-24T13:20:49Z")

</div>

I'm writing a library that uses trait `Bar` from crate `foo`. I'm trying to re-export `Bar` in my crate, so users can use my library without adding `foo` to their `cargo.toml`.

Unfortunatlely, the derive macro for the trait uses the fully qualified path `::foo::Bar`, so to derive it the user still needs to add the dependency.

I have written a new derive macro that uses `::my_library::Bar` instead, but now I cannot export it, because the name conflicts with the original macro.

Is there a standard way to approach this problem, or are my users forced to depend on the other crate to use the trait?

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [April 24, 2026, 1:31pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767/2 "2026-04-24T13:31:54Z")

</div>

Maybe the `foo::Bar` macro already implements a way to change the crate name used in the generated code? Like [`tokio::main`](https://docs.rs/tokio/latest/tokio/attr.main.html#rename-package) does, for example? Then you don't need your own derive macro. If not, most likely (as I understand your description) `foo::Bar` is a re-export of `foo_derive::Bar`. These re-exports are often feature-gated (e.g. `serde`'s `derive` feature). If you provide your own `Bar` derive macro, I don't see why you should also export `foo::Bar`. Disabling the feature of `foo` would resolve the name conflict.\[1\]

* * *

1. If you need the `Bar` macro internally in your crate, you can still import it directly from `foo_derive`.

---

<div class="post-metadata">

**Author:** ![KayJay7](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kayjay7/32/52281_2.png) [@KayJay7](https://users.rust-lang.org/u/KayJay7)\
**Post date:** [April 24, 2026, 8:42pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767/3 "2026-04-24T20:42:47Z")

</div>

Thanks for the help. I did not realize that the macro was behind a feature flag, but I double checked after your comment, and it was. In case you are interested the trait was [this one](https://docs.rs/frozen-collections/latest/frozen_collections/trait.Scalar.html).

So, I guess the standard approach to keep derive macros behind a feature flag, right?

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [April 24, 2026, 9:02pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767/4 "2026-04-24T21:02:10Z")

</div>

> [@KayJay7](#):
>
> So, I guess the standard approach to keep derive macros behind a feature flag, right?

Yes, I'd say re-exporting derive macros behind a feature flag is idiomatic API design. Compiling proc-macro crates is quite expensive, mostly due to `syn`. Users that don't need the macro and are conscious about compile times will appreciate being able to opt out of having to compile the unused proc-macro crate.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 23, 2026, 9:02pm UTC](https://users.rust-lang.org/t/re-exporting-a-trait-with-a-custom-derive-macro/139767/5 "2026-07-23T21:02:18Z")

</div>

Closed on July 23, 2026, 9:02pm UTC

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.
