可以关闭的重新导出可选货物功能

Re-export optional cargo feature that can be turned off

目录结构如下:

tree hello_dep
.
├── Cargo.lock
├── Cargo.toml
├── dep_a
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── dep_b
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── src
    └── main.rs

以及以下依赖链:hello_dep -> dep_a -> dep_b -> (optional feature) rustc-serialize, 我想在 dep_a 中创建一个功能,重新导出 dep_b 中可选的 rustc-serialize 功能。

在底部,我有 dep_b,它具有 rustc-serialize 作为可选的默认功能:

# dep_b/Cargo.toml
[package]
name = "dep_b"
version = "0.1.0"

[dependencies]
rustc-serialize = { version = "0.3.19", optional = true }

[features]
default = ["rustc-serialize"]

我想在 dep_a 中创建一个功能以选择性地重新导出 "rustc-serialize"。这是尝试:

# dep_a/Cargo.toml
[package]
name = "dep_a"
version = "0.1.0"

[dependencies]
dep_b = { version = "0.1.0", path = "../dep_b" }

[features]
rustc-serialize = ["dep_b/rustc-serialize"]
default = ["rustc-serialize"]

但是,当我尝试使用以下 Cargo.toml:

将其添加为默认关闭的依赖项时
# hello_dep/Cargo.toml
[package]
name = "hello_dep"
version = "0.1.0"

[dependencies]
dep_a = { version = "0.1.0", path = "dep_a", default-features = false, optional = true }

cargo build 仍然在 Cargo.lock 中生成 rustc-serialize。但是直接依赖 dep_b 正确地避免了使用以下行

引入 rustc-serialize

dep_b = { version = "0.1.0", path = "dep_b", default-features = false }

这是 Cargo 中的错误,还是我做错了什么?这是一个

dep_a/Cargo.toml 中,您没有在 dep_b 依赖项上指定 default-features = false。因此,默认启用 dep_b 中的 rustc-serialize 功能。您在 dep_a 中包含一项功能以启用 dep_brustc-serialize 的事实不会改变当 dep_a 的功能未启用时它仍然启用的事实。

因此,在 dep_a/Cargo.toml 中,您应该:

[dependencies]
dep_b = { version = "0.1.0", path = "../dep_b", default-features = false }