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

Compile-time equality assertion with formatting.

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

Comparison Arguments

This macro accepts these types for comparison and debug printing:

  • Standard library types for which PWrapper wrapping that type has a const_eq method. This includes all integer types, &str, slices/arrays of integers/&str, Options of integers/&str, etc.

  • non-standard-library types that implement FormatMarker with debug formatting
    and have a const fn const_eq(&self, other:&Self) -> bool inherent method,

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_eq;

use std::mem::size_of;

assertc_eq!(size_of::<usize>(), size_of::<[usize;1]>());

const TWO: u32 = 2;
assertc_eq!(TWO, TWO, "Oh no {} doesn't equal itself!!", TWO);

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_eq;

use std::mem::size_of;

assertc_eq!(size_of::<u32>(), size_of::<u8>());

This is the compiler output:

error[E0080]: evaluation of constant value failed
 --> src/macros/assertions.rs:296:13
  |
9 | assertc_eq!(size_of::<u32>(), size_of::<u8>());
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left == right)`
 left: `4`
right: `1`', src/macros/assertions.rs:9:13

Comparing user-defined types

This example demonstrates how you can assert that two values of a user-defined type are equal.

 #![feature(const_mut_refs)]

 use const_format::{Formatter, PWrapper};
 use const_format::{ConstDebug, assertc_eq, try_};

 const POINT: Point = Point{ x: 5, y: 8, z: 13 };
 const OTHER_POINT: Point = Point{ x: 21, y: 34, z: 55 };

 assertc_eq!(POINT, OTHER_POINT);

 #[derive(ConstDebug)]
 pub struct Point {
     pub x: u32,
     pub y: u32,
     pub z: u32,
 }

 impl Point {
     pub const fn const_eq(&self, other: &Self) -> bool {
         self.x == other.x &&
         self.y == other.y &&
         self.z == other.z
     }
 }

This is the compiler output:

error[E0080]: evaluation of constant value failed
  --> src/macros/assertions.rs:331:14
   |
12 |  assertc_eq!(POINT, OTHER_POINT);
   |              ^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left == right)`
 left: `Point {
    x: 5,
    y: 8,
    z: 13,
}`
right: `Point {
    x: 21,
    y: 34,
    z: 55,
}`', src/macros/assertions.rs:12:14

error: aborting due to previous error