This crate provide macros assertions who can be used in const
function.
```rust const fn myconstfn(x: u8) -> u8 { cfn_assert!(x < 5); x + 1 }
const CONST: u8 = myconst_fn(1);
fn main() { let var = myconst_fn(2); } ```
The function below panic when running :
rust
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 :
rust
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
.
This crate is available
on crates.io and can be used by
adding the following to your project's
Cargo.toml
:
toml
[dependencies]
const_fn_assert = "0.1"
and this to your crate root (main.rs
or lib.rs
):
```rust
extern crate constfnassert; ```