#const #function #assert #macro #testing

deprecated no-std const_fn_assert

Assertions for const functions

4 releases

0.1.3+deprecated May 8, 2023
0.1.2 Nov 26, 2019
0.1.1 Nov 9, 2019
0.1.0 Nov 9, 2019

#125 in #assert

Download history 1546/week @ 2023-12-15 523/week @ 2023-12-22 1144/week @ 2023-12-29 1744/week @ 2024-01-05 1111/week @ 2024-01-12 1664/week @ 2024-01-19 1489/week @ 2024-01-26 904/week @ 2024-02-02 962/week @ 2024-02-09 977/week @ 2024-02-16 808/week @ 2024-02-23 912/week @ 2024-03-01 1125/week @ 2024-03-08 939/week @ 2024-03-15 1162/week @ 2024-03-22 632/week @ 2024-03-29

3,984 downloads per month
Used in 3 crates

MIT license

7KB

const_fn_assert

Build Status Crate Documentation Minimum rustc version License


Note: As of Rust 1.57 this crate is superseded by native support for panic! in const contexts. Only consider using this crate if you care about supporting compilers between 1.31 and 1.57.

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