8
8
#![ feature( repr_simd) ]
9
9
10
10
use std:: cell:: { UnsafeCell , RefCell , Cell } ;
11
+ use std:: mem:: size_of;
11
12
use std:: num:: NonZeroU32 as N32 ;
12
13
use std:: sync:: { Mutex , RwLock } ;
13
14
@@ -22,11 +23,13 @@ struct Size<const S: usize>;
22
23
23
24
// Overwriting the runtime assertion and making it a compile-time assertion
24
25
macro_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>( ) } >;
27
28
} } ;
28
29
}
29
30
31
+ const PTR_SIZE : usize = std:: mem:: size_of :: < * const ( ) > ( ) ;
32
+
30
33
fn main ( ) {
31
34
assert_size ! ( Option <Wrapper <u32 >>, 8 ) ;
32
35
assert_size ! ( Option <Wrapper <N32 >>, 4 ) ; // (✓ niche opt)
@@ -38,25 +41,25 @@ fn main() {
38
41
assert_size ! ( Option <UnsafeCell <u32 >>, 8 ) ;
39
42
assert_size ! ( Option <UnsafeCell <N32 >>, 8 ) ; // (✗ niche opt)
40
43
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)
56
59
57
60
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)
60
63
61
64
#[ repr( simd) ]
62
65
pub struct Vec4 < T > ( [ T ; 4 ] ) ;
0 commit comments