@@ -23,58 +23,50 @@ struct NoNiche<T>(UnsafeCell<T>);
23
23
struct Size < const S : usize > ;
24
24
25
25
// Overwriting the runtime assertion and making it a compile-time assertion
26
- macro_rules! assert_size {
27
- ( $a: ty, $b: expr) => {
28
- const _: Size :: <{ $b} > = Size :: <{ size_of:: <$a>( ) } >;
26
+ macro_rules! assert_size_eq {
27
+ ( $ty: ty, $size: expr) => {
28
+ const _: Size :: <{ $size} > = Size :: <{ size_of:: <$ty>( ) } >;
29
+ } ;
30
+ ( $ty: ty, $size: expr, $optioned_size: expr) => {
31
+ assert_size_eq!( $ty, $size) ;
32
+ assert_size_eq!( Option <$ty>, $optioned_size) ;
33
+ const _: ( ) = assert!( $size == $optioned_size || size_of:: <$ty>( ) < size_of:: <Option <$ty>>( ) ) ;
29
34
} ;
30
35
}
31
36
32
37
const PTR_SIZE : usize = std:: mem:: size_of :: < * const ( ) > ( ) ;
33
38
34
- assert_size ! ( Option <Wrapper <u32 >>, 8 ) ;
35
- assert_size ! ( Option <Wrapper <N32 >>, 4 ) ; // (✓ niche opt)
36
- assert_size ! ( Option <Transparent <u32 >>, 8 ) ;
37
- assert_size ! ( Option <Transparent <N32 >>, 4 ) ; // (✓ niche opt)
38
- assert_size ! ( Option <NoNiche <u32 >>, 8 ) ;
39
- assert_size ! ( Option <NoNiche <N32 >>, 8 ) ; // (✗ niche opt)
40
-
41
- assert_size ! ( Option <UnsafeCell <u32 >>, 8 ) ;
42
- assert_size ! ( Option <UnsafeCell <N32 >>, 8 ) ; // (✗ niche opt)
43
-
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 ! (
39
+ assert_size_eq ! ( Wrapper <u32 >, 4 , 8 ) ;
40
+ assert_size_eq ! ( Wrapper <N32 >, 4 , 4 ) ; // (✓ niche opt)
41
+ assert_size_eq ! ( Transparent <u32 >, 4 , 8 ) ;
42
+ assert_size_eq ! ( Transparent <N32 >, 4 , 4 ) ; // (✓ niche opt)
43
+ assert_size_eq ! ( NoNiche <u32 >, 4 , 8 ) ;
44
+ assert_size_eq ! ( NoNiche <N32 >, 4 , 8 ) ;
45
+
46
+ assert_size_eq ! ( UnsafeCell <u32 >, 4 , 8 ) ;
47
+ assert_size_eq ! ( UnsafeCell <N32 >, 4 , 8 ) ;
48
+
49
+ assert_size_eq ! ( UnsafeCell <& ( ) > , PTR_SIZE , PTR_SIZE * 2 ) ;
50
+ assert_size_eq ! ( Cell <& ( ) > , PTR_SIZE , PTR_SIZE * 2 ) ;
51
+ assert_size_eq ! ( RefCell <& ( ) > , PTR_SIZE * 2 , PTR_SIZE * 3 ) ;
52
+ assert_size_eq ! (
51
53
RwLock <& ( ) >,
52
- if cfg!( target_pointer_width = "32" ) { 16 } else { 24 }
53
- ) ;
54
- assert_size ! (
55
- Option <RwLock <& ( ) >>,
54
+ if cfg!( target_pointer_width = "32" ) { 16 } else { 24 } ,
56
55
if cfg!( target_pointer_width = "32" ) { 20 } else { 32 }
57
- ) ; // (✗ niche opt)
58
- assert_size ! (
59
- Mutex <& ( ) > ,
60
- if cfg!( target_pointer_width = "32" ) { 12 } else { 16 }
61
56
) ;
62
- assert_size ! (
63
- Option <Mutex <& ( ) >>,
57
+ assert_size_eq ! (
58
+ Mutex <& ( ) > ,
59
+ if cfg!( target_pointer_width = "32" ) { 12 } else { 16 } ,
64
60
if cfg!( target_pointer_width = "32" ) { 16 } else { 24 }
65
- ) ; // (✗ niche opt)
61
+ ) ;
66
62
67
- assert_size ! ( UnsafeCell <& [ i32 ] > , PTR_SIZE * 2 ) ;
68
- assert_size ! ( Option <UnsafeCell <& [ i32 ] >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
69
- assert_size ! ( UnsafeCell <( & ( ) , & ( ) ) > , PTR_SIZE * 2 ) ;
70
- assert_size ! ( Option <UnsafeCell <( & ( ) , & ( ) ) >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
63
+ assert_size_eq ! ( UnsafeCell <& [ i32 ] > , PTR_SIZE * 2 , PTR_SIZE * 3 ) ;
64
+ assert_size_eq ! ( UnsafeCell <( & ( ) , & ( ) ) > , PTR_SIZE * 2 , PTR_SIZE * 3 ) ;
71
65
72
66
trait Trait { }
73
- assert_size ! ( UnsafeCell <& dyn Trait > , PTR_SIZE * 2 ) ;
74
- assert_size ! ( Option <UnsafeCell <& dyn Trait >>, PTR_SIZE * 3 ) ; // (✗ niche opt)
67
+ assert_size_eq ! ( UnsafeCell <& dyn Trait > , PTR_SIZE * 2 , PTR_SIZE * 3 ) ;
75
68
76
69
#[ repr( simd) ]
77
70
pub struct Vec4 < T > ( [ T ; 4 ] ) ;
78
71
79
- assert_size ! ( UnsafeCell <Vec4 <N32 >> , 16 ) ;
80
- assert_size ! ( Option <UnsafeCell <Vec4 <N32 >>>, 32 ) ; // (✗ niche opt)
72
+ assert_size_eq ! ( UnsafeCell <Vec4 <N32 >> , 16 , 32 ) ;
0 commit comments