如何判断每个板条箱有哪些可用的“功能”?

问题描述 投票:0回答:5

是否有标准方法来确定给定板条箱可以使用哪些功能?

我正在尝试读取 Postgres 时区,并且 this 表示使用板条箱

postgres = "0.17.0-alpha.1"
板条箱的
with-time
with-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

上会有一些关于它的内容?

rust rust-cargo rust-crates
5个回答
41
投票
显示存在哪些功能标志

。 crates.io issue #465 建议将功能列表也放在 crate 的页面上。 除此之外,查看可用功能的唯一有保证的方法是查看 crate 的 Cargo.toml。这通常意味着您需要导航到项目的存储库,找到您感兴趣的版本的正确文件,然后阅读它。

您主要寻找

[features]

部分,同时也寻找标记为

optional = true
的任何依赖项,因为
可选依赖项计为隐式功能标志
不幸的是,不要求包作者提供有关每个功能标志的作用的任何文档。好的板条箱会在一个或多个位置记录其功能标志:

作为 Cargo.toml 中的评论
  • 他们的自述文件
  • 他们的文档
  • 另请参阅:

    如何启用 Rust“板条箱功能”?
对于 postgres crate,我们可以
从 crates.io 开始

,然后单击“存储库”转到存储库。然后我们找到正确的标签(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"]



11
投票
cargo-feature

箱来查看和管理依赖项的功能: > 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

板条箱以编程方式检索依赖项的功能集: 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 add

cargo feature
等其他工具提供依赖项和功能信息的方式。
    


0
投票

cargo add postgres --dry-run

它将打印可用/已安装的功能而不更改任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.