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

Compile-time equality 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_eq;

const NAME: &str = "Bob";

assertcp_eq!(NAME, "Bob", "Guessed wrong, the right value is {}", NAME);

const SUM: u8 = 1 + 2 + 3;
assertcp_eq!(6u8, SUM, "Guessed wrong, the right value is {}", SUM);

Failing assertion

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

use const_format::assertcp_eq;

use std::mem::size_of;

#[repr(C)]
struct Type(u16, u16, u16);

assertcp_eq!(size_of::<Type>(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large");

This is the compiler output:

error[E0080]: evaluation of constant value failed
  --> src/macros/assertions/assertcp_macros.rs:226:14
   |
11 | assertcp_eq!(size_of::<Type>(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large");
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left == right)`
 left: `6`
right: `4`
Whoops, `Type` is too large
', src/macros/assertions/assertcp_macros.rs:11:14