#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

#407 in Rust patterns

Download history 870/week @ 2021-06-30 494/week @ 2021-07-07 358/week @ 2021-07-14 654/week @ 2021-07-21 418/week @ 2021-07-28 1064/week @ 2021-08-04 1269/week @ 2021-08-11 1589/week @ 2021-08-18 2057/week @ 2021-08-25 2686/week @ 2021-09-01 2958/week @ 2021-09-08 3007/week @ 2021-09-15 2139/week @ 2021-09-22 2920/week @ 2021-09-29 2598/week @ 2021-10-06 3146/week @ 2021-10-13

4,151 downloads per month
Used in 6 crates (5 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