[][src]Crate const_fn

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

Examples

When using like the following functions to control unstable features:

[features]
const_unstable = []

It can be written as follows:

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

pub struct Foo<T> {
    x: T,
}

impl<T: Iterator> Foo<T> {
    /// Constructs a new `Foo`.
    #[const_fn(feature = "const_unstable")]
    pub const fn new(x: T) -> Self {
        Self { x }
    }
}

Code like this will be generated:

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

pub struct Foo<T> {
    x: T,
}

impl<T: Iterator> Foo<T> {
    /// Constructs a new `Foo`.
    #[cfg(feature = "const_unstable")]
    pub const fn new(x: T) -> Self {
        Self { x }
    }

    /// Constructs a new `Foo`.
    #[cfg(not(feature = "const_unstable"))]
    pub fn new(x: T) -> Self {
        Self { x }
    }
}

See test_suite for more examples.

Attribute Macros

const_fn

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