Macro const_format::assertc

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

Compile-time assertions with formatting.

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 formatc macro.

Limitations

This macro has these limitations:

Examples

Passing assertion

#![feature(const_mut_refs)]

use const_format::assertc;

use std::mem::size_of;

assertc!(
    size_of::<&str>() == size_of::<&[u8]>(),
    "The size of `&str`({} bytes) and `&[u8]`({} bytes) aren't the same?!?!",
    size_of::<&str>(),
    size_of::<&[u8]>(),
);

Failing assertion

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

#![feature(const_mut_refs)]

use const_format::assertc;

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

assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L,  R);

This is the compiler output:

error[E0080]: evaluation of constant value failed
  --> src/macros/assertions.rs:132:10
   |
12 | assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L,  R);
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed.
2 plus 2 isn't 5, buddy
', src/macros/assertions.rs:12:10