0

我试过了:

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

它似乎忽略了第一个之后的任何特征,默默地。

这会导致错误,因为在这种情况下,编译器似乎不相信 Foo 实现了 BazTrait。


更新:@kmdreko 的代码只要BazTraitFoo.

When BazTraitis 在另一个 crate 中,它也使用enum_dispatch,BazTrait被忽略并导致以下形式的两个错误:

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`

#[enum_dispatch(BarTrait, BazTrait)]重要的是要注意或都没有错误pub enum Foo {

4

1 回答 1

2

#[enum_dispatch]属性不适用于 crate:

不幸的是,Rust 中的过程宏在某些方面受到限制,这将使您试图实现的目标变得不可能 - 特别是,它们是独立运行的 per-crate,因此来自一个 crate 的信息无法影响另一个 crate 中的实现。

来自 enum_dispatch 的作者在一个不相关的问题中。

于 2021-05-16T05:44:46.020 回答