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