logo
macro_rules! assert_eq_size {
    ($x:ty, $($xs:ty),+ $(,)?) => { ... };
}
Expand description

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. That is where this macro comes into play.

Alternatives

There also exists 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

These three types, despite being very different, all have the same size:

assert_eq_size!([u8; 4], (u16, u16), u32);

The following example fails to compile because u32 has 4 times the size of u8:

assert_eq_size!(u32, u8);