#macro #assert #const #function #testing

no-std const_fn_assert

Assertions for const functions

3 releases

0.1.2 Nov 26, 2019
0.1.1 Nov 9, 2019
0.1.0 Nov 9, 2019

#850 in Rust patterns

Download history 1263/week @ 2022-04-14 1532/week @ 2022-04-21 1377/week @ 2022-04-28 1652/week @ 2022-05-05 2054/week @ 2022-05-12 1966/week @ 2022-05-19 1481/week @ 2022-05-26 1383/week @ 2022-06-02 1512/week @ 2022-06-09 1414/week @ 2022-06-16 1731/week @ 2022-06-23 1695/week @ 2022-06-30 1909/week @ 2022-07-07 1933/week @ 2022-07-14 2266/week @ 2022-07-21 2033/week @ 2022-07-28

8,592 downloads per month
Used in 7 crates (4 directly)

MIT license

6KB

const_fn_assert

Build Status Crate Documentation Minimum rustc version License

This crate provide macros assertions who can be used in const function.

Example

const fn my_const_fn(x: u8) -> u8 {
    cfn_assert!(x < 5);
    x + 1
}

const _CONST: u8 = my_const_fn(1);

fn main() {
    let _var = my_const_fn(2);
}

The function below panic when running :

fn fail() {
    let _var = my_const_fn(6); //thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1'
}

And this code don't compile :

const _CONST: u8 = my_const_fn(6); //~ ERROR any use of this value will cause an error

Available macros are cfn_assert, cfn_assert_eq, cfn_assert_ne, cfn_debug_assert, cfn_debug_assert_eq and cfn_debug_assert_ne.

Installation

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

[dependencies]
const_fn_assert = "0.1"

and this to your crate root (main.rs or lib.rs):

#[macro_use]
extern crate const_fn_assert;

No runtime deps