88#![ feature( repr_simd) ]
99
1010use std:: cell:: { UnsafeCell , RefCell , Cell } ;
11+ use std:: mem:: size_of;
1112use std:: num:: NonZeroU32 as N32 ;
1213use std:: sync:: { Mutex , RwLock } ;
1314
@@ -22,11 +23,13 @@ struct Size<const S: usize>;
2223
2324// Overwriting the runtime assertion and making it a compile-time assertion
2425macro_rules! assert_size {
25- ( $a: ty, $b: literal ) => { {
26- const _: Size :: <$b > = Size :: <{ std :: mem :: size_of:: <$a>( ) } >;
26+ ( $a: ty, $b: expr ) => { {
27+ const _: Size :: <{ $b } > = Size :: <{ size_of:: <$a>( ) } >;
2728 } } ;
2829}
2930
31+ const PTR_SIZE : usize = std:: mem:: size_of :: < * const ( ) > ( ) ;
32+
3033fn main ( ) {
3134 assert_size ! ( Option <Wrapper <u32 >>, 8 ) ;
3235 assert_size ! ( Option <Wrapper <N32 >>, 4 ) ; // (✓ niche opt)
@@ -38,25 +41,25 @@ fn main() {
3841 assert_size ! ( Option <UnsafeCell <u32 >>, 8 ) ;
3942 assert_size ! ( Option <UnsafeCell <N32 >>, 8 ) ; // (✗ niche opt)
4043
41- assert_size ! ( UnsafeCell <& ( ) > , 8 ) ;
42- assert_size ! ( Option <UnsafeCell <& ( ) >>, 16 ) ; // (✗ niche opt)
43- assert_size ! ( Cell <& ( ) > , 8 ) ;
44- assert_size ! ( Option < Cell <& ( ) >>, 16 ) ; // (✗ niche opt)
45- assert_size ! ( RefCell <& ( ) > , 16 ) ;
46- assert_size ! ( Option < RefCell <& ( ) >>, 24 ) ; // (✗ niche opt)
47- assert_size ! ( RwLock <& ( ) > , 24 ) ;
48- assert_size ! ( Option < RwLock <& ( ) >>, 32 ) ; // (✗ niche opt)
49- assert_size ! ( Mutex <& ( ) > , 16 ) ;
50- assert_size ! ( Option < Mutex <& ( ) >>, 24 ) ; // (✗ niche opt)
51-
52- assert_size ! ( UnsafeCell <& [ i32 ] > , 16 ) ;
53- assert_size ! ( Option <UnsafeCell <& [ i32 ] >>, 24 ) ; // (✗ niche opt)
54- assert_size ! ( UnsafeCell <( & ( ) , & ( ) ) > , 16 ) ;
55- assert_size ! ( Option <UnsafeCell <( & ( ) , & ( ) ) >>, 24 ) ; // (✗ niche opt)
44+ assert_size ! ( UnsafeCell <& ( ) > , PTR_SIZE ) ;
45+ assert_size ! ( Option <UnsafeCell <& ( ) >>, PTR_SIZE * 2 ) ; // (✗ niche opt)
46+ assert_size ! ( Cell <& ( ) > , PTR_SIZE ) ;
47+ assert_size ! ( Option < Cell <& ( ) >>, PTR_SIZE * 2 ) ; // (✗ niche opt)
48+ assert_size ! ( RefCell <& ( ) > , PTR_SIZE * 2 ) ;
49+ assert_size ! ( Option < RefCell <& ( ) >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
50+ assert_size ! ( RwLock <& ( ) > , if cfg! ( target_pointer_width = "32" ) { 16 } else { 24 } ) ;
51+ assert_size ! ( Option < RwLock <& ( ) >>, if cfg! ( target_pointer_width = "32" ) { 20 } else { 32 } ) ; // (✗ niche opt)
52+ assert_size ! ( Mutex <& ( ) > , if cfg! ( target_pointer_width = "32" ) { 12 } else { 16 } ) ;
53+ assert_size ! ( Option < Mutex <& ( ) >>, if cfg! ( target_pointer_width = "32" ) { 16 } else { 24 } ) ; // (✗ niche opt)
54+
55+ assert_size ! ( UnsafeCell <& [ i32 ] > , PTR_SIZE * 2 ) ;
56+ assert_size ! ( Option <UnsafeCell <& [ i32 ] >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
57+ assert_size ! ( UnsafeCell <( & ( ) , & ( ) ) > , PTR_SIZE * 2 ) ;
58+ assert_size ! ( Option <UnsafeCell <( & ( ) , & ( ) ) >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
5659
5760 trait Trait { }
58- assert_size ! ( UnsafeCell <& dyn Trait > , 16 ) ;
59- assert_size ! ( Option <UnsafeCell <& dyn Trait >>, 24 ) ; // (✗ niche opt)
61+ assert_size ! ( UnsafeCell <& dyn Trait > , PTR_SIZE * 2 ) ;
62+ assert_size ! ( Option <UnsafeCell <& dyn Trait >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
6063
6164 #[ repr( simd) ]
6265 pub struct Vec4 < T > ( [ T ; 4 ] ) ;
0 commit comments