Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(const_fn)]
- const fn align_of<T>(_: &T) -> usize {
- ::std::mem::size_of::<T>()
- }
- // fails to compile if the types of $x and $y don't have the same alignment
- macro_rules! assert_eq_align {
- ($x:expr, $y:expr) => (
- // don't actually evaluate at runtime
- if false { const_assert!(align_of(&$x) == align_of(&$y)); }
- ($x, $y)
- )
- }
- // copied from static_assertions crate
- macro_rules! const_assert {
- ($($xs:expr),+ $(,)*) => {
- #[allow(unknown_lints, eq_op)]
- let _ = [(); 0 - !($($xs)&&+) as usize];
- };
- ($label:ident; $($xs:tt)+) => {
- #[allow(dead_code, non_snake_case)]
- fn $label() { const_assert!($($xs)+); }
- };
- }
- fn main() {
- let (a, b) = assert_eq_align!(0u8, 0u16);
- }
Add Comment
Please, Sign In to add comment