Last active
December 5, 2017 06:36
-
-
Save nvzqz/f3de36b61b277ab789314710c1f0b150 to your computer and use it in GitHub Desktop.
Assert equal type size in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem::transmute; | |
macro_rules! assert_eq_size { | |
($x:ty, $($xs:ty),+ $(,)*) => { | |
$(let _ = transmute::<$x, $xs>;)+ | |
}; | |
($label:ident; $($rest:tt)+) => { | |
#[allow(dead_code, non_snake_case)] | |
fn $label() { | |
assert_eq_size!($($rest)+); | |
} | |
}; | |
} | |
assert_eq_size! { some_sizes; | |
[u8; 4], | |
(u16, u16), | |
u32, | |
} | |
fn main() { | |
// Change me to any other number | |
const N: usize = 2; | |
assert_eq_size!(u32, [u16; N]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is used as an example within https://nikolaivazquez.com/posts/programming/rust-static-assertions/