rust - 二进制操作 == 不能应用于类型 syn::Path

标签 rust compiler-errors dependencies rust-cargo

当我使用 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 的 PartialEqEq 特征实现在“完整”或“派生”功能门之后,但我仍然不知道。

明确尝试了 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 在启用 fullderive 功能时可用,为该类型实现的一些特征不是。

尤其是 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/

相关文章:

buffer - 获取字节数组的读取器

arrays - 如何克隆长度大于 32 的数组?

arrays - 通过函数修改结构的固定数组

rust - 多次借用的借用错误

compiler-errors - 为什么在方法签名中使用 hyper::Client 时会出现错误 "wrong number of type arguments"?

gradle - 对于特定依赖项,Gradle覆盖一般的transitive = false

java - “找不到符号”或“无法解析符号”错误是什么意思?

c++ - Thrift TNonblockingServer.cpp未声明的标识符-Windows

node.js - node.js 上的依赖版本冲突

java - 尝试使用 SWT 会导致许多错误