Skip to main content

Crate cfg_iif

Crate cfg_iif 

Source
Expand description

A macro for defining #[cfg] if-else functions.

The macro provided by this crate. Unlike cfg_if, cfg_iif can be used as a function, and can be used in a function.

§Features

  • minimum support rustc 1.60.0 (7737e0b5c 2022-04-04)
  • support else if chains
  • support multiple cfg predicates (implicitly combined with all())

§Example

§Example 1: #[cfg()]

  • a_iif is “unix” when a os is Unix at compile time:
use cfg_iif::cfg_iif;
let a_iif = cfg_iif!(#[cfg(target_family = "unix")] { "unix" } else { "not unix" });
  • a_iif is “abc” when a feature is “has_abc” at compile time:
use cfg_iif::cfg_iif;
let a_iif = cfg_iif!(#[cfg(feature = "has_abc")] { "abc" } else { "not abc" });
  • Using else if chains:
use cfg_iif::cfg_iif;
let result = cfg_iif!(
    #[cfg(target_os = "linux")] { "linux" }
    else if #[cfg(target_os = "windows")] { "windows" }
    else { "other" }
);
  • Using multiple predicates:
use cfg_iif::cfg_iif;
let result = cfg_iif!(
    #[cfg(unix, target_pointer_width = "64")] { "64-bit unix" }
    else { "other" }
);

§Example 2: a short hand for a firendly cargo fmt

  • a_iif is “abc” when a feature is “has_abc” at compile time:
use cfg_iif::cfg_iif;
let a_iif = cfg_iif!(feature = "has_abc" { "abc" } else { "not abc" });
  • Using else if chains with shorthand:
use cfg_iif::cfg_iif;
let result = cfg_iif!(
    target_os = "linux" { "linux" }
    else if target_os = "windows" { "windows" }
    else { "other" }
);

Macros§

cfg_iif
This macro provided by this crate. See crate documentation for more information.