是否有标准方法来确定给定板条箱可以使用哪些功能?
我正在尝试读取 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。 crates.io issue #465 建议将功能列表也放在 crate 的页面上。 除此之外,查看可用功能的唯一有保证的方法是查看 crate 的 Cargo.toml。这通常意味着您需要导航到项目的存储库,找到您感兴趣的版本的正确文件,然后阅读它。
您主要寻找
[features]
部分,同时也寻找标记为
optional = true
的任何依赖项,因为 可选依赖项计为隐式功能标志。 不幸的是,不要求包作者提供有关每个功能标志的作用的任何文档。好的板条箱会在一个或多个位置记录其功能标志:
作为 Cargo.toml 中的评论
对于 postgres crate,我们可以
,然后单击“存储库”转到存储库。然后我们找到正确的标签(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"]
箱来查看和管理依赖项的功能:
> 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
添加其他人也这么认为!这是在 2020 年底
到docs.rs。您可以通过导航到顶部栏上的“功能标志”来查看包文档中的功能列表:
postgres 0.17.0的功能列表,因为向网站添加新功能时不会重新生成旧文档,但任何最近发布的版本都将提供该文档。
注意:这仅在 docs.rs 上可用,并且在运行时不会生成cargo doc
。
板条箱以编程方式检索依赖项的功能集:
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
等其他工具提供依赖项和功能信息的方式。cargo add postgres --dry-run
它将打印可用/已安装的功能而不更改任何内容。