35

Is there a standard way to determine what features are available for a given crate?

I'm trying to read Postgres timezones, and this says to use the crate postgres = "0.17.0-alpha.1" crate's with-time or with-chrono features.

When I try this in my Cargo.toml:

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

I get this error:

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.

Furthermore, the crate page for postgres 0.17.0 says nothing about these features, so I don't even know if they're supposed to be supported or not.

It seems like there would be something on docs.rs about it?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
stuart
  • 1,785
  • 2
  • 26
  • 38

4 Answers4

39

Crates uploaded to crates.io (and thus to docs.rs) will show what feature flags exist. crates.io issue #465 suggests placing the feature list on the crate's page as well.

Beyond that, the only guaranteed way to see what features are available is to look at the Cargo.toml for the crate. This generally means that you need to navigate to the project's repository, find the correct file for the version you are interested in, and read it.

You are primarily looking for the [features] section, but also for any dependencies that are marked as optional = true, as optional dependencies count as an implicit feature flag.

Unfortunately, there's no requirement that the crate author puts any documentation about what each feature flag does. Good crates will document their feature flags in one or more locations:

  • As comments in Cargo.toml
  • Their README
  • Their documentation

See also:


For the postgres crate, we can start at crates.io, then click "repository" to go to the repository. We then find the right tag (postgres-v0.17.0), then read the 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"]
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
9

You can use the cargo-feature crate to view and manage your dependencies' features:

> 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"]

Note: this only works if the crate is already in Cargo.toml as a dependency.

The list of features are also displayed when using 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
kmdreko
  • 42,554
  • 6
  • 57
  • 106
melMass
  • 3,813
  • 1
  • 31
  • 30
4

It seems like there would be something on docs.rs about it?

Other people thought so too! This was added to docs.rs in late 2020. You can view the list of features from the crate's documentation by navigating to "Feature flags" on the top bar:

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

You still won't see the list of features of postgres 0.17.0 though since old documentation isn't regenerated when new functionality is added to the site, but any recently published versions will have it available.

Note: this is only available on docs.rs and not generated when running cargo doc.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
2

The feature-set of a dependency can be retrieved programmatically via the 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",
    ],
}

This is how other tools like cargo add and cargo feature provide dependency and feature information.

kmdreko
  • 42,554
  • 6
  • 57
  • 106