const_fn 0.2.0

An attribute for easy generation of a const function with conditional compilations.
Documentation

#[const_fn]

Build Status version documentation license Rustc Version

An attribute for easy generation of a const function with conditional compilations.

Usage

Add this to your Cargo.toml:

[dependencies]
const_fn = "0.2"

The current const_fn requires Rust 1.31 or later.

Examples

When using like the following functions to control unstable features:

[features]
const = []

It can be written as follows:

#![cfg_attr(feature = "const", feature(const_fn, const_vec_new))]
use const_fn::const_fn;

#[const_fn(feature = "const")]
pub const fn empty_vec<T>() -> Vec<T> {
    Vec::new()
}

Code like this will be generated:

#![cfg_attr(feature = "const", feature(const_fn, const_vec_new))]

#[cfg(feature = "const")]
pub const fn empty_vec<T>() -> Vec<T> {
    Vec::new()
}

#[cfg(not(feature = "const"))]
pub fn empty_vec<T>() -> Vec<T> {
    Vec::new()
}

See test_suite for more examples.

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.