• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-564424

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

CHANGELOG.mdH A D01-Jan-19704.9 KiB165106

Cargo.tomlH A D01-Jan-19701 KiB3027

Cargo.toml.orig-cargoH A D01-Jan-1970591 2420

LICENSE-APACHEH A D01-Jan-19709.9 KiB178150

LICENSE-MITH A D01-Jan-19701,023 2421

README.mdH A D01-Jan-19702.9 KiB9268

build.rsH A D01-Jan-19704.1 KiB12795

README.md

1# \#\[const_fn\]
2
3[![crates.io](https://img.shields.io/crates/v/const_fn?style=flat-square&logo=rust)](https://crates.io/crates/const_fn)
4[![docs.rs](https://img.shields.io/badge/docs.rs-const__fn-blue?style=flat-square)](https://docs.rs/const_fn)
5[![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue?style=flat-square)](#license)
6[![rustc](https://img.shields.io/badge/rustc-1.31+-blue?style=flat-square&logo=rust)](https://www.rust-lang.org)
7[![build status](https://img.shields.io/github/workflow/status/taiki-e/const_fn/CI/main?style=flat-square&logo=github)](https://github.com/taiki-e/const_fn/actions)
8
9An attribute for easy generation of const functions with conditional
10compilations.
11
12## Usage
13
14Add this to your `Cargo.toml`:
15
16```toml
17[dependencies]
18const_fn = "0.4"
19```
20
21*Compiler support: requires rustc 1.31+*
22
23## Examples
24
25```rust
26use const_fn::const_fn;
27
28// function is `const` on specified version and later compiler (including beta, nightly, and dev build)
29#[const_fn("1.36")]
30pub const fn version() {
31    /* ... */
32}
33
34// function is `const` on nightly compiler (including dev build)
35#[const_fn(nightly)]
36pub const fn nightly() {
37    /* ... */
38}
39
40// function is `const` if `cfg(...)` is true
41#[const_fn(cfg(...))]
42pub const fn cfg() {
43    /* ... */
44}
45
46// function is `const` if `cfg(feature = "...")` is true
47#[const_fn(feature = "...")]
48pub const fn feature() {
49    /* ... */
50}
51```
52
53### Use this crate as an optional dependency
54
55If no arguments are passed, `consf_fn` will always make the function `const`.
56
57Therefore, you can use `const_fn` as an optional dependency by combination with `cfg_attr`.
58
59```rust
60// function is `const` if `cfg(feature = "...")` is true
61#[cfg_attr(feature = "...", const_fn::const_fn)]
62pub fn optional() {
63    /* ... */
64}
65```
66
67<!--
68TODO: document the behavior on the version on the nightly channel.
69      https://github.com/taiki-e/const_fn/issues/27
70      https://github.com/rust-lang/rust/pull/81468
71-->
72
73## Alternatives
74
75This crate is proc-macro, but is very lightweight, and has no dependencies.
76
77You can manually define declarative macros with similar functionality (see
78[`if_rust_version`](https://github.com/ogoffart/if_rust_version#examples)),
79or [you can define the same function twice with different cfg](https://github.com/crossbeam-rs/crossbeam/blob/0b6ea5f69fde8768c1cfac0d3601e0b4325d7997/crossbeam-epoch/src/atomic.rs#L340-L372).
80(Note: the former approach requires more macros to be defined depending on the
81number of version requirements, the latter approach requires more functions to
82be maintained manually)
83
84## License
85
86Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
87[MIT license](LICENSE-MIT) at your option.
88
89Unless you explicitly state otherwise, any contribution intentionally submitted
90for inclusion in the work by you, as defined in the Apache-2.0 license, shall
91be dual licensed as above, without any additional terms or conditions.
92