rust toml:如何有条件地启用依赖特性?

gopyfrb3  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(126)

在我的项目中,我有两个特点:myfeatureAmyfeatureB
我希望其中一个依赖于tokio,具有rtsync特性,另一个依赖于tokio,仅具有sync特性。
我在Cargo.toml中尝试了这个配置:

[dependencies]
tokio = { version = "1.32", features = ["rt", "sync"], optional = true }

[features]
myfeatureA = ["dep:tokio"]
myfeatureB = ["dep:tokio/sync"]
$ cargo build --features=myfeatureB
error: failed to parse manifest at `[...]/myproject/Cargo.toml`

Caused by:
  feature `myfeatureB` includes `dep:tokio/sync` with both `dep:` and `/`
  To fix this, remove the `dep:` prefix.

所以,我删除了dep:前缀(这是这个other question提供的解决方案,但它不起作用):

[features]
myfeatureA = ["dep:tokio"]
myfeatureB = ["tokio/sync"]
$ cargo build --features=myfeatureB
error: Package `myproject v0.1.0 ([...]/myproject)` does not have feature `tokio`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name.

如何使myfeatureB仅依赖于tokiosync特性?

l3zydbqr

l3zydbqr1#

这在issue #10788中报告过,Rust 1.72.0修复了这个问题。现在您可以使用第二种方法(无dep:)。
对于以前的版本,根本不使用dep:,即使是其他功能。不过,这会创建一个名为tokio的功能。

dwbf0jvd

dwbf0jvd2#

完全不使用dep:(与文档建议的相反)可以工作:

[dependencies]
tokio = { version = "1.32", features = ["rt", "sync"], optional = true }

[features]
myfeatureA = ["tokio"]
myfeatureB = ["tokio/sync"]

我猜dep:机制还没有完全实现。
编辑:使用Rust 1.72,来自原始文章的第二个例子(参考):

[features]
myfeatureA = ["dep:tokio"]
myfeatureB = ["tokio/sync"]

相关问题