[][src]Crate const_fn_assert

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

Examples

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

const _CONST: u8 = my_const_fn(1);

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

Inputs are type-checked as booleans:

This example deliberately fails to compile
fn main() {
    cfn_assert!(!0);
}

Despite this being a macro, we see this produces a type error:

  | cfn_assert!(!0);
  |             ^^ expected bool, found integral variable
  |
  = note: expected type `bool`
             found type `{integer}`

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 :

This example deliberately fails to compile
const _CONST: u8 = my_const_fn(6); //~ ERROR any use of this value will cause an error

Advices

Since the panic message is not really descriptive, it is advisable to create a non-constant version of your functions using normal assertions.

Macros

cfn_assert
cfn_assert_eq
cfn_assert_ne
cfn_debug_assert
cfn_debug_assert_eq
cfn_debug_assert_ne