如何使用 enum_dispatch 的两个特征?
How to use two traits with enum_dispatch?
我试过:
#[enum_dispatch(BarTrait, BazTrait)]
pub enum Foo {
VariantZero,
...
}
似乎忽略了第一个之后的任何特征,默默地。
这会导致错误,因为在这种情况下,编译器似乎不相信 Foo 实现了 BazTrait。
更新:只要 BazTrait
与 Foo
.
在同一个箱子中,@kmdreko 的 code 就可以正常工作
当 BazTrait
在不同的 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 {
处没有错误。
#[enum_dispatch]
属性不适用于 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.
来自 enum_dispatch 的作者 unrelated issue。
我试过:
#[enum_dispatch(BarTrait, BazTrait)]
pub enum Foo {
VariantZero,
...
}
似乎忽略了第一个之后的任何特征,默默地。
这会导致错误,因为在这种情况下,编译器似乎不相信 Foo 实现了 BazTrait。
更新:只要 BazTrait
与 Foo
.
当 BazTrait
在不同的 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 {
处没有错误。
#[enum_dispatch]
属性不适用于 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.
来自 enum_dispatch 的作者 unrelated issue。