Expand description
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:
ⓘ
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 :
ⓘ
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§
Constants§
- ASSERT
- Used by the macros of this crate to check the assertions.
Functions§
- bool_
assert - Used by the macros of this crate to check that the inputs are boolean.