当我使用 my fork of async-trait 时作为依赖项,由于 syn::*
类型相等,它无法编译。 All is green in async-trait CI checks .要重现,请启动一个新的 cargo lib 项目并添加到 Cargo.toml:
[dependencies]
syn = { version = "1.0.39", features = ["full"] }
在 lib.rs 中:
pub fn cmp(a: syn::Path, b: syn::Path) -> bool {
a == b
}
在 Rust 1.46.0 上编译会导致错误:
error[E0369]: binary operation `==` cannot be applied to type `syn::Path`
--> src/lib.rs:4:7
|
4 | a == b
| - ^^ - syn::Path
| |
| syn::Path
error: aborting due to previous error
syn::Path
implements Eq
/PartialEq
with feature "full" or "derive" :
use syn; // 1.0.33
fn cmp(a: syn::Path, b: syn::Path) -> bool {
a == b
}
我探索了 syn 的 PartialEq
和 Eq
特征实现在“完整”或“派生”功能门之后,但我仍然不知道。
明确尝试了 1.0.33 版,该版本在 Playground 中运行,在我的 PC 上结果相同。
我已经完成了将 async-trait 拆分并重新组合在一起的障碍,但这超出了我的技能范围。
- rustc 1.46.0 (04488afe3 2020-08-24)
- cargo 1.46.0 (149022b1d 2020-07-17)
cargo tree
使用 syn:
tmp v0.1.0 (/home/debian/Documents/Projects/tmp)
└── syn v1.0.39
├── proc-macro2 v1.0.19
│ └── unicode-xid v0.2.1
├── quote v1.0.7
│ └── proc-macro2 v1.0.19 (*)
└── unicode-xid v0.2.1
最佳答案
虽然 type syn::Path
在启用 full
或 derive
功能时可用,为该类型实现的一些特征不是。
尤其是 as per syn
's documentation of optional features , extra-traits
特性是获取 PartialEq
所必需的:
extra-traits
— Debug, Eq, PartialEq, Hash impls for all syntax tree types.
因此你只需要调整你的 Cargo.toml
与
syn = { version = "1.0.39", features = ["full", "extra-traits"] }
关于rust - 二进制操作 == 不能应用于类型 syn::Path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63677722/