0

I've tried:

#[enum_dispatch(BarTrait, BazTrait)]
pub enum Foo {
    VariantZero,
    ...
}

It seems to ignore any traits after the first, silently.

This causes errors, as in this case the compiler doesn't seem to believe that Foo implements BazTrait.


Update: @kmdreko's code works properly so long as BazTrait is in the same crate as Foo.

When BazTrait is in a different crate, which also uses enum_dispatch, BazTrait is ignored and causes two errors of the form:

error[E0599]: no method named `baz` found for enum `Foo` in the current scope
  --> src/main.rs:53:9
   |
45 | enum Foo {
   | -------- method `baz` not found for this
...
53 |     foo.baz();
   |         ^^^ method not found in `Foo`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `baz`, perhaps you need to implement it:
           candidate #1: `mylib::BazTrait`

It is important to note that there is no error at either #[enum_dispatch(BarTrait, BazTrait)] or pub enum Foo {.

fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    Can you make a more complete example? [This code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b60f1386df38cbd080ec89b3c426a6d9) seems to work just fine. – kmdreko May 15 '21 at 17:03
  • Thanks for the example. I'll post a more complete example in the morning. I didn't know whether `enum_dispatch` was even meant to work with multiple traits, so thanks for confirming. – fadedbee May 15 '21 at 21:16
  • I can confirm that your code works fine, when it's all in the same crate. I've updated the question with new observations. – fadedbee May 16 '21 at 05:09

1 Answers1

3

The #[enum_dispatch] attribute does not work across crates:

Unfortunately, procedural macros in Rust are limited in a few ways that will make what you're trying to achieve impossible - notably, they are run independently per-crate, so there's no way for information from one crate to affect an implementation in another.

From the author of enum_dispatch in an unrelated issue.

kmdreko
  • 42,554
  • 6
  • 57
  • 106