如何确定每个crate提供了哪些“features”?

39

有没有一种标准方法可以确定给定的crate有哪些功能可用?

我正在尝试读取Postgres时区,并且这个说要使用crate postgres = "0.17.0-alpha.1",并使用crate的with-timewith-chrono功能。

当我在我的Cargo.toml中尝试时:

[dependencies]
postgres = { version = "0.17.0-alpha.1", features = ["with-time"] }

我遇到了这个错误:

error: failed to select a version for `postgres`.
    ... required by package `mypackage v0.1.0 (/Users/me/repos/mypackage)`
versions that meet the requirements `^0.17.0-alpha.1` are: 0.17.0, 0.17.0-alpha.2, 0.17.0-alpha.1

the package `mypackage` depends on `postgres`, with features: `with-time` but `postgres` does not have these features.
此外,Postgres 0.17.0的crate页面并没有提到这些功能,因此我甚至不知道它们是否应该得到支持。
看起来应该有docs.rs上的相关内容吧?
5个回答

44
上传到crates.io(因此也上传到docs.rs)的crate将显示有哪些功能标志。 crates.io issue #465 建议在crate页面上放置功能列表。
除此之外,查看crate的Cargo.toml是查看可用功能的唯一保证方式。这通常意味着您需要导航到项目的存储库,找到您感兴趣的版本的正确文件并阅读它。
您主要寻找 [features] 部分,但还要寻找任何标记为 optional = true 的依赖项,因为可选依赖项计为隐式功能标志
不幸的是,没有要求crate作者放置有关每个功能标志的文档。好的crate将在一个或多个位置记录其功能标志:
  • 作为Cargo.toml中的注释
  • 他们的README
  • 他们的文档
另请参见:
针对postgres crate,我们可以从crates.io开始,然后点击“repository”进入存储库。我们找到正确的标签(postgres-v0.17.0),然后阅读Cargo.toml
[features]
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
with-geo-types-0_4 = ["tokio-postgres/with-geo-types-0_4"]
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]

1
如果可以的话,我会给这个点赞10次。这种知识是无价的,感谢@Shepmaster。 - Alan Sikora
这太棒了!谢谢你分享这个。 - Frankely Diaz

11
你可以使用 cargo-feature crate 来查看和管理你的依赖特性:
> cargo install cargo-feature --locked

> cargo feature postgres
   Avaliable features for `postgres`
array-impls = ["tokio-postgres/array-impls"]
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
with-eui48-1 = ["tokio-postgres/with-eui48-1"]
with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"]
with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
with-smol_str-01 = ["tokio-postgres/with-smol_str-01"]
with-time-0_2 = ["tokio-postgres/with-time-0_2"]
with-time-0_3 = ["tokio-postgres/with-time-0_3"]
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
with-uuid-1 = ["tokio-postgres/with-uuid-1"]

注意:只有在包已经作为依赖项存在于 Cargo.toml 中时,此方法才有效。

在使用 cargo add 命令时,还会显示功能列表:

> cargo add postgres
    Updating crates.io index
      Adding postgres v0.19.4 to dependencies.
             Features:
             - array-impls
             - with-bit-vec-0_6
             - with-chrono-0_4
             - with-eui48-0_4
             - with-eui48-1
             - with-geo-types-0_6
             - with-geo-types-0_7
             - with-serde_json-1
             - with-smol_str-01
             - with-time-0_2
             - with-time-0_3
             - with-uuid-0_8
             - with-uuid-1

4

看起来在docs.rs上应该有相关内容?

其他人也这样认为!这个功能在2020年末添加到了docs.rs上。您可以通过导航到顶部栏中的“功能标志”来查看该包文档中的功能列表:

The feature flags page for the postgres crate on docs.rs

尽管在网站添加新功能时旧文档不会重新生成,但最近发布的版本将提供postgres 0.17.0的功能列表。

注意:此功能仅在docs.rs上可用,而在运行cargo doc时不会生成。


2

可以通过 cargo-metadata crate 程序化地检索依赖项的功能集:

use cargo_metadata::MetadataCommand;

fn main() {
    let metadata = MetadataCommand::new()
        .exec()
        .expect("failed to get metadata");
    
    let dependency = metadata.packages
        .iter()
        .find(|package| package.name == "postgres")
        .expect("failed to find dependency");

    println!("{:#?}", dependency.features);
}

{
    "with-time-0_3": [
        "tokio-postgres/with-time-0_3",
    ],
    "with-smol_str-01": [
        "tokio-postgres/with-smol_str-01",
    ],
    "with-chrono-0_4": [
        "tokio-postgres/with-chrono-0_4",
    ],
    "with-uuid-0_8": [
        "tokio-postgres/with-uuid-0_8",
    ],
    "with-uuid-1": [
        "tokio-postgres/with-uuid-1",
    ],
    "array-impls": [
        "tokio-postgres/array-impls",
    ],
    "with-eui48-1": [
        "tokio-postgres/with-eui48-1",
    ],
    "with-bit-vec-0_6": [
        "tokio-postgres/with-bit-vec-0_6",
    ],
    "with-geo-types-0_6": [
        "tokio-postgres/with-geo-types-0_6",
    ],
    "with-geo-types-0_7": [
        "tokio-postgres/with-geo-types-0_7",
    ],
    "with-eui48-0_4": [
        "tokio-postgres/with-eui48-0_4",
    ],
    "with-serde_json-1": [
        "tokio-postgres/with-serde_json-1",
    ],
    "with-time-0_2": [
        "tokio-postgres/with-time-0_2",
    ],
}

这就是其他工具如 cargo addcargo feature 提供依赖和特性信息的方式。

0
如melMass所说,运行
cargo add postgres --dry-run

而且它会打印出可用/已安装的功能,而不会改变任何内容。

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