如何在Cargo中为子依赖项指定功能?

14

我正在开发一个使用reqwest和self_update的CLI应用程序。 self_update也使用reqwest。 我希望我的应用程序使用rustls而不是拉取openssl依赖项。 Cargo.toml允许选择依赖项的功能

[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或任何其他高度可配置的非直接依赖包的功能?


1
功能应该是可添加的。如果您在自己的依赖项中启用了一个功能,那么它也会在您的子依赖项中可用。您不会在应用程序中同时获得带有和不带有该功能的两个reqwest副本。 - mcarton
好的,但是我如何删除默认功能?我该如何防止self_update拉取openssl依赖项,这是reqwest的默认功能? - Cameron Taggart
2
self_update本身必须具备此功能。Cargo无法知道从子依赖项中删除功能是否安全,因为您的依赖项可能实际上使用该功能。 - mcarton
我考虑给 self_update 添加一个 rustls 特性,但是 cargo 不支持基于特性的不同依赖项。https://github.com/rust-lang/cargo/issues/5954 我认为将每个可选特性都向上传递到消费库会是一种奇怪的设计。 - Cameron Taggart
1
我在 https://github.com/seanmonstar/reqwest/pull/902 上询问了这个问题。 - Cameron Taggart
self_update 添加了一个重新导出 reqwest 功能的特性。https://github.com/jaemk/self_update/pull/43/files 问题已解决! - Cameron Taggart
1个回答

11

我同意你的观点,如果能够支持子依赖项功能将会非常好。

虽然不是最理想的解决方案,但如果您将子依赖项添加为主要依赖项,则该功能将会起作用。

[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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接