Macro const_format::assertc_ne
source · macro_rules! assertc_ne { ($($parameters:tt)*) => { ... }; }
assertc
only.Expand description
Compile-time inequality 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 aconst_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 aconst 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:
-
It can only use constants that involve concrete types, so while a
Type::<u8>::FOO
in an argument would be fine,Type::<T>::FOO
would not be (T
being a type parameter). -
Integer arguments must have a type inferrable from context, as described in the integer arguments section in the root module .
Examples
Passing assertion
#![feature(const_mut_refs)]
use const_format::assertc_ne;
use std::mem::size_of;
assertc_ne!(size_of::<u32>(), size_of::<[u32; 2]>());
const TWO: u32 = 2;
const THREE: u32 = 3;
assertc_ne!(TWO, THREE, "Oh no {} somehow equals {}!!", TWO, THREE);
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_ne;
use std::mem::size_of;
type Foo = u32;
assertc_ne!(size_of::<u32>(), size_of::<Foo>());
This is the compiler output:
error[E0080]: evaluation of constant value failed
--> src/macros/assertions.rs:465:13
|
11 | assertc_ne!(size_of::<u32>(), size_of::<Foo>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left != right)`
left: `4`
right: `4`', src/macros/assertions.rs:11:13
Comparing user-defined types
This example demonstrates how you can assert that two values of a user-defined type are unequal.
#![feature(const_mut_refs)]
use const_format::{Formatter, PWrapper};
use const_format::{ConstDebug, assertc_ne, try_};
const POINT: Point = Point{ x: 5, y: 8, z: 13 };
assertc_ne!(POINT, 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:451:14
|
11 | assertc_ne!(POINT, POINT);
| ^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: `(left != right)`
left: `Point {
x: 5,
y: 8,
z: 13,
}`
right: `Point {
x: 5,
y: 8,
z: 13,
}`', src/macros/assertions.rs:11:14
error: aborting due to previous error