cfg-if
An if/elif-like macro for chaining Rust #[cfg] conditional-compilation branches
Repository Health
Technical Analysis
cfg-if is a small, dependency-free Rust crate maintained under the rust-lang organization that provides a single cfg_if! macro for ergonomically expressing a chain of #[cfg(...)] conditional-compilation branches. Instead of duplicating an item under separate #[cfg]/#[cfg(not(...))] attributes for every platform or feature combination, cfg_if! lets you write an if/else-if/else chain and have the macro expand it into the correct mutually-exclusive #[cfg] attributes.
It is one of the most widely depended-on crates in the Rust ecosystem (over a billion downloads), used throughout the standard library’s own dependency graph and by low-level crates like libc, winapi-adjacent crates, and cross-platform runtime crates that need per-target or per-feature code paths.
What You Get
- The
cfg_if!macro supporting arbitrarily long if/else-if/else chains of#[cfg(...)]predicates - Automatic expansion into mutually exclusive
#[cfg]/#[cfg(not(any(...)))]attributes so branches never conflict - Zero required runtime dependencies — the crate compiles to essentially nothing at runtime, it is a compile-time-only macro
- An optional
rustc-dep-of-stdfeature enabling the crate to be used inside the Rust standard library’s own build - no_std compatibility by design, since the crate has no runtime component at all
Common Use Cases
- Low-level systems crates selecting a different implementation per target OS (Unix vs Windows vs WASM) without duplicating function signatures under multiple #[cfg] attributes
- Crates with optional Cargo features that need to swap an implementation in/out (e.g. a
stdvsno_stdcode path) - Cross-platform FFI or syscall wrapper crates picking the correct binding set for the current target_arch/target_os combination
- Any crate maintaining several feature-gated code paths that would otherwise require repetitive #[cfg(not(any(…)))] boilerplate
Under The Hood
Architecture: The entire crate is a single src/lib.rs file (212 lines) containing one macro_rules! definition for cfg_if!. The macro recursively parses an if/else-if/else token sequence and, for each branch, accumulates the negations of all preceding predicates so the emitted #[cfg] attributes are provably non-overlapping — this recursive-descent-over-tokens approach is the entire implementation. Tech Stack: Pure Rust 2018 edition, MSRV 1.32, with a single optional dependency (rustc-std-workspace-core, gated behind the rustc-dep-of-std feature used only when the crate is vendored into the Rust standard library’s own build graph) — no other runtime or build dependencies exist. Code Quality: Because the crate is a single declarative macro with no runtime logic, correctness is validated through the macro’s own doctested examples and downstream usage across the ecosystem (a billion-plus downloads act as an implicit integration test suite); the macro_rules! pattern matching is dense but the file is small enough to audit in full in minutes. API Design: The public surface is exactly one macro invoked with familiar if/else-if/else syntax mirroring ordinary Rust control flow, so there is effectively zero learning curve for anyone who already knows #[cfg] attributes — this minimalism is the crate’s entire value proposition.
Used by 7 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
CubeSandbox
Developer Tools · Security · AI Agents
Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.
GitButler
Developer Tools · Devops · AI Development
Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.
hoop
Security · Monitoring
A wire-protocol gateway that enforces data masking, command blocking, approval workflows, and full session recording for engineers and AI agents accessing production infrastructure.
melty
Developer Tools · AI Code Assistants · Code Editors
The AI code editor where every chat message is a git commit you can revert, branch, or squash
RustDesk
Networking
Open-source, self-hosted remote desktop built in Rust — your data, your infrastructure, no third-party cloud.
Void
AI Code Assistants · Code Editors · Automation
Open-source AI code editor with direct LLM integration and data privacy