cfg-version
Conditional compilation based on dependency versions.
Usage
[]
= "0.1"
use cfg_version;
Version syntax
Uses Cargo's semver syntax:
| Syntax | Example | Meaning |
|---|---|---|
^ (default) |
"^2.8" or "2.8" |
>= 2.8.0, < 3.0.0 |
~ |
"~2.8" |
>= 2.8.0, < 2.9.0 |
>= |
">=2.8" |
>= 2.8.0 |
> |
">2.8" |
> 2.8.0 |
< |
"<3" |
< 3.0.0 |
<= |
"<=2.8" |
<= 2.8.0 |
= |
"=2.8.0" |
exactly 2.8.0 |
* |
"*" |
any version |
| combined | ">=2.8, <2.12" |
>= 2.8.0 and < 2.12.0 |
How it works
At macro expansion time, cfg-version:
- Reads
CARGO_PKG_NAMEto identify the current crate. - Finds
Cargo.lockby walking up fromCARGO_MANIFEST_DIR. - Parses the lock file to find the current crate's direct dependencies and their resolved versions.
- Matches the resolved version against the user-specified requirement.
- Keeps or removes the annotated item accordingly.
When a dependency appears multiple times in Cargo.lock (e.g., syn 1.x and 2.x), only the version directly depended on by the current crate is considered.
License
MIT