Simplify size checking

This commit is contained in:
Oli Scherer 2022-07-11 14:23:32 +00:00
parent d935c70afa
commit cdd6bba8f6

View File

@ -23,58 +23,50 @@
struct Size<const S: usize>;
// Overwriting the runtime assertion and making it a compile-time assertion
macro_rules! assert_size {
($a:ty, $b:expr) => {
const _: Size::<{$b}> = Size::<{size_of::<$a>()}>;
macro_rules! assert_size_eq {
($ty:ty, $size:expr) => {
const _: Size::<{$size}> = Size::<{size_of::<$ty>()}>;
};
($ty:ty, $size:expr, $optioned_size:expr) => {
assert_size_eq!($ty, $size);
assert_size_eq!(Option<$ty>, $optioned_size);
const _: () = assert!($size == $optioned_size || size_of::<$ty>() < size_of::<Option<$ty>>());
};
}
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
assert_size!(Option<Wrapper<u32>>, 8);
assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
assert_size!(Option<Transparent<u32>>, 8);
assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt)
assert_size!(Option<NoNiche<u32>>, 8);
assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
assert_size_eq!(Wrapper<u32>, 4, 8);
assert_size_eq!(Wrapper<N32>, 4, 4); // (✓ niche opt)
assert_size_eq!(Transparent<u32>, 4, 8);
assert_size_eq!(Transparent<N32>, 4, 4); // (✓ niche opt)
assert_size_eq!(NoNiche<u32>, 4, 8);
assert_size_eq!(NoNiche<N32>, 4, 8);
assert_size!(Option<UnsafeCell<u32>>, 8);
assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
assert_size_eq!(UnsafeCell<u32>, 4, 8);
assert_size_eq!(UnsafeCell<N32>, 4, 8);
assert_size!( UnsafeCell<&()> , PTR_SIZE);
assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
assert_size!( Cell<&()> , PTR_SIZE);
assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
assert_size!( RefCell<&()> , PTR_SIZE * 2);
assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt)
assert_size!(
assert_size_eq!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2);
assert_size_eq!( Cell<&()> , PTR_SIZE, PTR_SIZE * 2);
assert_size_eq!( RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3);
assert_size_eq!(
RwLock<&()>,
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
);
assert_size!(
Option<RwLock<&()>>,
if cfg!(target_pointer_width = "32") { 16 } else { 24 },
if cfg!(target_pointer_width = "32") { 20 } else { 32 }
); // (✗ niche opt)
assert_size!(
Mutex<&()> ,
if cfg!(target_pointer_width = "32") { 12 } else { 16 }
);
assert_size!(
Option<Mutex<&()>>,
assert_size_eq!(
Mutex<&()> ,
if cfg!(target_pointer_width = "32") { 12 } else { 16 },
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
); // (✗ niche opt)
);
assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2);
assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt)
assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2);
assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt)
assert_size_eq!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
assert_size_eq!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
trait Trait {}
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
assert_size_eq!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
#[repr(simd)]
pub struct Vec4<T>([T; 4]);
assert_size!( UnsafeCell<Vec4<N32>> , 16);
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
assert_size_eq!(UnsafeCell<Vec4<N32>> , 16, 32);