#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

#586 in Rust patterns

Download history 4698/week @ 2022-12-03 3656/week @ 2022-12-10 2683/week @ 2022-12-17 2928/week @ 2022-12-24 3474/week @ 2022-12-31 5706/week @ 2023-01-07 4015/week @ 2023-01-14 4352/week @ 2023-01-21 4196/week @ 2023-01-28 5722/week @ 2023-02-04 5232/week @ 2023-02-11 5501/week @ 2023-02-18 5740/week @ 2023-02-25 5857/week @ 2023-03-04 5523/week @ 2023-03-11 4599/week @ 2023-03-18

22,560 downloads per month
Used in 21 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