Simplify assertion macro

This commit is contained in:
Oli Scherer 2022-07-11 10:30:50 +00:00
parent 3f4cf59323
commit af8536e32f

View File

@ -8,7 +8,6 @@
#![feature(repr_simd)]
use std::cell::{UnsafeCell, RefCell, Cell};
use std::mem::size_of;
use std::num::NonZeroU32 as N32;
use std::sync::{Mutex, RwLock};
@ -21,45 +20,45 @@
// Overwriting the runtime assertion and making it a compile-time assertion
macro_rules! assert_eq {
($a:expr, $b:literal) => {{
const _: () = assert!($a == $b);
($a:ty, $b:literal) => {{
const _: () = assert!(std::mem::size_of::<$a>() == $b);
}};
}
fn main() {
assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);
assert_eq!(size_of::<Option<Wrapper<N32>>>(), 4); // (✓ niche opt)
assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
assert_eq!(size_of::<Option<Transparent<N32>>>(), 4); // (✓ niche opt)
assert_eq!(size_of::<Option<NoNiche<u32>>>(), 8);
assert_eq!(size_of::<Option<NoNiche<N32>>>(), 8); // (✗ niche opt)
assert_eq!(Option<Wrapper<u32>>, 8);
assert_eq!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
assert_eq!(Option<Transparent<u32>>, 8);
assert_eq!(Option<Transparent<N32>>, 4); // (✓ niche opt)
assert_eq!(Option<NoNiche<u32>>, 8);
assert_eq!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
assert_eq!(size_of::<Option<UnsafeCell<u32>>>(), 8);
assert_eq!(size_of::<Option<UnsafeCell<N32>>>(), 8); // (✗ niche opt)
assert_eq!(Option<UnsafeCell<u32>>, 8);
assert_eq!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
assert_eq!(size_of::< UnsafeCell<&()> >(), 8);
assert_eq!(size_of::<Option<UnsafeCell<&()>>>(), 16); // (✗ niche opt)
assert_eq!(size_of::< Cell<&()> >(), 8);
assert_eq!(size_of::<Option< Cell<&()>>>(), 16); // (✗ niche opt)
assert_eq!(size_of::< RefCell<&()> >(), 16);
assert_eq!(size_of::<Option< RefCell<&()>>>(), 24); // (✗ niche opt)
assert_eq!(size_of::< RwLock<&()> >(), 24);
assert_eq!(size_of::<Option< RwLock<&()>>>(), 32); // (✗ niche opt)
assert_eq!(size_of::< Mutex<&()> >(), 16);
assert_eq!(size_of::<Option< Mutex<&()>>>(), 24); // (✗ niche opt)
assert_eq!( UnsafeCell<&()> , 8);
assert_eq!(Option<UnsafeCell<&()>>, 16); // (✗ niche opt)
assert_eq!( Cell<&()> , 8);
assert_eq!(Option< Cell<&()>>, 16); // (✗ niche opt)
assert_eq!( RefCell<&()> , 16);
assert_eq!(Option< RefCell<&()>>, 24); // (✗ niche opt)
assert_eq!( RwLock<&()> , 24);
assert_eq!(Option< RwLock<&()>>, 32); // (✗ niche opt)
assert_eq!( Mutex<&()> , 16);
assert_eq!(Option< Mutex<&()>>, 24); // (✗ niche opt)
assert_eq!(size_of::< UnsafeCell<&[i32]> >(), 16);
assert_eq!(size_of::<Option<UnsafeCell<&[i32]>>>(), 24); // (✗ niche opt)
assert_eq!(size_of::< UnsafeCell<(&(), &())> >(), 16);
assert_eq!(size_of::<Option<UnsafeCell<(&(), &())>>>(), 24); // (✗ niche opt)
assert_eq!( UnsafeCell<&[i32]> , 16);
assert_eq!(Option<UnsafeCell<&[i32]>>, 24); // (✗ niche opt)
assert_eq!( UnsafeCell<(&(), &())> , 16);
assert_eq!(Option<UnsafeCell<(&(), &())>>, 24); // (✗ niche opt)
trait Trait {}
assert_eq!(size_of::< UnsafeCell<&dyn Trait> >(), 16);
assert_eq!(size_of::<Option<UnsafeCell<&dyn Trait>>>(), 24); // (✗ niche opt)
assert_eq!( UnsafeCell<&dyn Trait> , 16);
assert_eq!(Option<UnsafeCell<&dyn Trait>>, 24); // (✗ niche opt)
#[repr(simd)]
pub struct Vec4<T>([T; 4]);
assert_eq!(size_of::< UnsafeCell<Vec4<N32>> >(), 16);
assert_eq!(size_of::<Option<UnsafeCell<Vec4<N32>>>>(), 32); // (✗ niche opt)
assert_eq!( UnsafeCell<Vec4<N32>> , 16);
assert_eq!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
}