Summary
Allow a crate to test whether a feature of an upstream dependency is activated, or not:
#[cfg(feature = "rustls/default-crypto-provider")]
Motivation
At the moment a crate can define its own feature, and should the user enable it, enable an upstream dependency feature in turn, for example:
[features]
default-crypto-provider = ["rustls/default-crypto-provider"]
It is not, however, possible to simple query whether an upstream crate feature is enabled.
And therefore, instead of library authors simply querying for whether an upstream crate feature is enabled, define hundreds of "clone" features in their libraries, which users must then discover, and enable for each independent dependency tree in their codebase.
Guide-level explanation
If you wish to know whether an upstream crate feature is enabled, simple prefix the feature by the crate name.
#[cfg(feature = "<upstream-crate>/<feature>")]
The usual warnings will be emitted if the upstream crate has no such feature, they can be silenced as usual.
Reference-level explanation
As above.
Drawbacks
A slightly more complicated cargo & rustc implementation, but really not by much.
Rationale and alternatives
Pre-declare in Cargo
At a very slight loss of ergonomics, due to added boilerplate, requiring that features to be queried be pre-declared in Cargo.toml would avoid having to pass 100s of useless features to a crate which has 100s of upstream crates.
Cargo-level Querying
An alternative would be to condition the enabling of a local feature based on the presence of an upstream feature, something like:
[features]
my-default-crypto-provider = []
[features.rustls]
default-crypto-provider = ["my-default-crypto-provider"]
This opens the door to fixpoint resolution of features, however, as enabling my-default-crypto-provider
could then in turn enable other crate features, which in turn could be queried, etc...
It may be useful, one day... but since it's overkill here, it's perhaps best avoiding.
Prior art
cargo-metadata in build script
There are existing work-around, today, using cargo-metadata to inspect a dependency's enabled features in a build.rs
.
This is, quite obviously, an abomination. And of course, it's not a fixpoint abomination, so it has bizarre shortcomings.
Features across workspaces
The ability to query the feature of upstream dependencies is more generic -- it works across workspaces -- and would solve the above at the cost of creating a root tikv-features
crate to define the common workspace features, then use those everywhere.
Alternatively, a special workspace/
prefix (or cargo-workspace
, or something) could be used to query workspace features, so as to not to have to create a crate just for it.
Unresolved questions
- Is pre-declaration in Cargo necessary?
Future possibilities
Enhanced workspace support
Introduce a special workspace/
or cargo-workspace/
prefix to allow a crate which is part of a workspace to query whether a feature is enabled at workspace level.
Besides the possible name collision, this would require extra work at publication time, as the workspace features would have to be replicated within each crate... which using a separate crate within the workspace to define the features does not suffer from.