Skip to main content

Crate cfg_version

Crate cfg_version 

Source
Expand description

Conditional compilation based on dependency versions.

Parses Cargo.lock at macro expansion time to determine the resolved version of any dependency, then conditionally includes or excludes the annotated item. When a dependency appears multiple times in the lock file (e.g., syn 1.x and 2.x), only the version actually used by the current crate is considered.

§Usage

# Cargo.toml
[dependencies]
cfg-version = "0.1"
use cfg_version::cfg_version;

// Include only when indexmap >= 2.8.0, < 3.0.0
#[cfg_version(indexmap = "^2.8")]
fn needs_indexmap_2_8() { }

// Include only when indexmap >= 2.0.0
#[cfg_version(indexmap = ">=2")]
fn needs_indexmap_2() { }

// Include only when indexmap < 2.8.0
#[cfg_version(indexmap = "<2.8")]
fn old_indexmap_fallback() { }

Version requirements use Cargo’s semver syntax: ^, ~, *, >=, >, <, <=, =, and comma-separated combinations.

Attribute Macros§

cfg_version
Conditionally include an item based on a dependency’s resolved version.