Crates.io | const_fn |
lib.rs | const_fn |
version | 0.4.10 |
source | src |
created_at | 2018-12-25 20:13:05.596834 |
updated_at | 2024-04-21 04:23:01.242098 |
description | A lightweight attribute for easy generation of const functions with conditional compilations. |
homepage | |
repository | https://github.com/taiki-e/const_fn |
max_upload_size | |
id | 103778 |
size | 45,464 |
A lightweight attribute for easy generation of const functions with conditional compilations.
Add this to your Cargo.toml
:
[dependencies]
const_fn = "0.4"
use const_fn::const_fn;
// function is `const` on specified version and later compiler (including beta, nightly, and dev build)
#[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() {
/* ... */
}
If no arguments are passed, const_fn
will always make the function const
.
Therefore, you can use const_fn
as an optional dependency by combination with cfg_attr
.
// function is `const` if `cfg(feature = "...")` is true
#[cfg_attr(feature = "...", const_fn::const_fn)]
pub fn optional() {
/* ... */
}
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)
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
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.