Import const_fn-0.4.2

Bug: 169611678
Test: make
Change-Id: I1afe31e048749baadd86d44ff19eee0e159b3d0d
27 files changed
tree: b9aad8dad8b06e64bc53f305f0c46134e4ecb718
  1. .cargo_vcs_info.json
  2. .editorconfig
  3. .gitattributes
  4. .github/
  5. .gitignore
  6. CHANGELOG.md
  7. Cargo.toml
  8. Cargo.toml.orig
  9. LICENSE-APACHE
  10. LICENSE-MIT
  11. METADATA
  12. MODULE_LICENSE_APACHE2
  13. OWNERS
  14. README.md
  15. build.rs
  16. ci/
  17. rustfmt.toml
  18. src/
README.md

#[const_fn]

crates-badge docs-badge license-badge rustc-badge

An attribute for easy generation of const functions with conditional compilations.

Usage

Add this to your Cargo.toml:

[dependencies]
const_fn = "0.4"

The current const_fn requires Rust 1.31 or later.

Examples

use const_fn::const_fn;

// function is `const` on specified version and later compiler (including beta and nightly)
#[const_fn("1.36")]
pub const fn version() {
    /* ... */
}

// function is `const` on nightly compiler (including dev build)
#[const_fn(nightly)]
pub const fn nightly() {
    /* ... */
}

// function is `const` if `cfg(...)` is true
#[const_fn(cfg(...))]
pub const fn cfg() {
    /* ... */
}

// function is `const` if `cfg(feature = "...")` is true
#[const_fn(feature = "...")]
pub const fn feature() {
    /* ... */
}

Alternatives

This crate is proc-macro, but is very lightweight, and has no dependencies.

You can manually define declarative macros with similar functionality (see if_rust_version), or you can define the same function twice with different cfg. (Note: the former approach requires more macros to be defined depending on the number of version requirements, the latter approach requires more functions to be maintained manually)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.