Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #![feature(const_fn)]
  2.  
  3. const fn align_of<T>(_: &T) -> usize {
  4. ::std::mem::size_of::<T>()
  5. }
  6.  
  7. // fails to compile if the types of $x and $y don't have the same alignment
  8. macro_rules! assert_eq_align {
  9. ($x:expr, $y:expr) => (
  10. // don't actually evaluate at runtime
  11. if false { const_assert!(align_of(&$x) == align_of(&$y)); }
  12. ($x, $y)
  13. )
  14. }
  15.  
  16. // copied from static_assertions crate
  17. macro_rules! const_assert {
  18. ($($xs:expr),+ $(,)*) => {
  19. #[allow(unknown_lints, eq_op)]
  20. let _ = [(); 0 - !($($xs)&&+) as usize];
  21. };
  22. ($label:ident; $($xs:tt)+) => {
  23. #[allow(dead_code, non_snake_case)]
  24. fn $label() { const_assert!($($xs)+); }
  25. };
  26. }
  27.  
  28. fn main() {
  29. let (a, b) = assert_eq_align!(0u8, 0u16);
  30. }
Add Comment
Please, Sign In to add comment