Macro static_assertions::assert_eq_size
source · macro_rules! assert_eq_size {
($($xs:tt)+) => { ... };
}
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
On stable Rust, using the macro requires a unique “label” when used in a module scope:
assert_eq_size!(bytes; (u8, u8), u16);
The labeling limitation is not necessary if
compiling on nightly Rust with the nightly
feature enabled:
ⓘ
#![feature(underscore_const_names)]
assert_eq_size!(u32, [u16; 2]);
fn main() {
// Supports unlimited arguments without hitting recursion limit
assert_eq_size!([u8; 4], (u16, u16), u32);
}
The following produces a compilation failure because u32
has 4 times the
size of u8
:
ⓘ
assert_eq_size!(u32, u8);