【问题标题】:Re-export optional cargo feature that can be turned off重新导出可关闭的可选货物功能
【发布时间】:2018-02-09 06:17:40
【问题描述】:

具有以下目录结构:

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 中的错误,还是我做错了什么?这是related question

【问题讨论】:

    标签: rust rust-cargo


    【解决方案1】:

    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 }
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2017-07-10
      相关资源
      最近更新 更多