如何为 Cargo 中的子依赖项指定功能? [英] How do I specify features for a sub-dependency in Cargo?

查看:64
本文介绍了如何为 Cargo 中的子依赖项指定功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a CLI app that uses reqwest and self_update. self_update also uses reqwest. I want my app to use rustls and not pull in openssl dependencies. Cargo.toml allows choosing features of dependencies:

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

如果子依赖能工作就好了:

It would be cool if sub-dependencies worked:

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

我还查看了替换部分,但只有这样的东西才能在我分支代码的地方工作:

I also looked at replace section, but only something like this works where I branch the code:

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

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

But what I want is default-features and features supported too:

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

How do I configure the features of Reqwest or Tokio or any other highly configurable non-direct dependency using Cargo?

推荐答案

我同意你的观点,支持子依赖特性会很棒.

I agree with you, having support for sub-dependencies features would be great.

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

It's not ideal but if you add your sub-dependency as a dependency the feature is going to work.

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

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

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

If you don't specify a version you'll get a deprecation warning, but at least you won't have to keep track of the sub-dependency version manually:

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

这篇关于如何为 Cargo 中的子依赖项指定功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆