【问题标题】:How do I specify features for a sub-dependency in Cargo?如何为 Cargo 中的子依赖项指定功能?
【发布时间】:2020-12-28 01:11:08
【问题描述】:

我正在开发一个使用 reqwest 和 self_update 的 CLI 应用程序。 self_update 也使用 reqwest。我希望我的应用程序使用 rustls 而不是引入 openssl 依赖项。 Cargo.toml 允许 choosing features 的依赖项:

[dependencies.reqwest]
version = "0.10"
default-features = false
features = ["rustls-tls", "json", "blocking"]

如果子依赖有效,那就太酷了:

[dependencies.self_update.reqwest]
version = "0.10"
default-features = false
features = ["rustls-tls", "json", "blocking"]

我还查看了replace section,但只有这样的东西在我分支代码的地方才有效:

"reqwest:0.10.1" = { branch = "rustls", git = "https://github.com/ctaggart/reqwest" }

但我想要的是默认功能和支持的功能:

"reqwest:0.10.1" = { tag="v0.10.1", git = "https://github.com/seanmonstar/reqwest", default-features = false, features = ["rustls-tls", "json", "blocking"] }

如何使用 Cargo 配置 Reqwest 或 Tokio 或任何其他高度可配置的非直接依赖项的功能?

【问题讨论】:

  • 特征是附加的。如果您在自己的依赖项中启用了一项功能,那么它也可以在您的子依赖项中使用。无论有没有该功能,您都不会在您的应用程序中获得两个 reqwest 副本。
  • 好的,但是如何删除默认功能?如何防止 self_update 引入 openssl 依赖项,这是 reqwest 的默认功能?
  • self_update 本身必须具有这样做的功能。 Cargo 无法知道从子依赖项中删除功能是否安全,因为您的依赖项实际上可能使用该功能。
  • 我想过给self_update添加一个rustls特性,但是cargo不支持基于特性的不同依赖。 github.com/rust-lang/cargo/issues/5954 我认为必须将每个可选功能都冒泡到消费库中是一种奇怪的设计。
  • 我在github.com/seanmonstar/reqwest/pull/902问过这个问题

标签: rust rust-cargo


【解决方案1】:

我同意你的观点,支持子依赖功能会很棒。

这并不理想,但是如果您将子依赖项添加为依赖项,则该功能将起作用。

[dependencies]
...
surf = "2.1.0" # which depends on http-client, which depends on isahc
...

[dependencies.isahc]
features = ["static-ssl"]

如果您不指定版本,您将收到弃用警告,但至少您不必手动跟踪子依赖版本:

warning: dependency (isahc) specified without providing a local path, Git repository, or version to use. This will be considered an error in future versions

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 2017-02-05
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多