macro_rules! assertcp {
    ($($parameters:tt)*) => { ... };
}
Available on crate feature assertcp only.
Expand description

Compile-time assertion with formatting.

For examples look here

This macro requires the “assertcp” feature to be exported.

Syntax

This macro uses the same syntax for the format string and formatting arguments as the [formatcp] macro.

Limitations

This macro can only take constants of these types as arguments:

  • &str

  • i*/u* (all the primitive integer types).

  • char

  • bool

This macro also has these limitations:

Examples

Passing assertion

use const_format::assertcp;

use std::mem::align_of;

assertcp!(
    align_of::<&str>() == align_of::<usize>(),
    "The alignment of `&str`({} bytes) and `usize`({} bytes) isn't the same?!?!",
    align_of::<&str>(),
    align_of::<usize>(),
);

Failing assertion

This example demonstrates a failing assertion, and how the compiler error looks like as of 2021-09-18.

use const_format::assertcp;

const L: u64 = 2;
const R: u32 = 5;

assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R));

This is the compiler output:

error[E0080]: evaluation of constant value failed
  --> src/macros/assertions/assertcp_macros.rs:124:11
   |
10 | assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R));
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed.
2 to the 5 isn't 64, it's 32
', src/macros/assertions/assertcp_macros.rs:10:11