Macro static_assertions::assert_eq_size [] [src]

macro_rules! assert_eq_size {
    ($x:ty, $($xs:ty),+ $(,)*) => { ... };
    ($label:ident; $($xs:tt)+) => { ... };
}

Asserts that types are equal in size.

When performing operations such as pointer casts or dealing with usize versus u64 versus u32, the size of your types matter. This is where this macro comes into play.

Alternatives

There are also assert_eq_size_val and assert_eq_size_ptr. Instead of specifying types to compare, values' sizes can be directly compared against each other.

Examples

// Can be declared outside of a function if labeled
assert_eq_size!(bytes; (u8, u8), u16);

fn main() {
    // Supports unlimited arguments:
    assert_eq_size!([u8; 4], (u16, u16), u32);
}

The following produces a compilation failure because u32 has 4 times the size of u8:

This code doesn't compile so be extra careful!
assert_eq_size!(u32, u8);