@@ -13,6 +13,8 @@ fn main() {
13
13
14
14
test_swap_ptr ( ) ;
15
15
16
+ test_init_interior_mutable ( ) ;
17
+
16
18
test_dangling ( ) ;
17
19
}
18
20
@@ -43,56 +45,61 @@ fn test_init_int() {
43
45
44
46
fn test_init_array ( ) {
45
47
extern "C" {
46
- fn init_array ( ptr : * mut i32 , len : usize , value : i32 ) ;
48
+ fn init_array ( ptr : * mut i32 , len : usize ) ;
47
49
}
48
50
49
- const LEN : usize = 4 ;
50
- let init_value = 41 ;
51
+ const LEN : usize = 3 ;
51
52
52
53
let mut array = MaybeUninit :: < [ i32 ; LEN ] > :: uninit ( ) ;
53
54
let array = unsafe {
54
- init_array ( array. as_mut_ptr ( ) . cast :: < i32 > ( ) , LEN , init_value ) ;
55
+ init_array ( array. as_mut_ptr ( ) . cast :: < i32 > ( ) , LEN ) ;
55
56
array. assume_init ( )
56
57
} ;
57
58
58
- assert_eq ! ( array, [ init_value ; LEN ] ) ;
59
+ assert_eq ! ( array, [ 31 ; LEN ] ) ;
59
60
}
60
61
61
62
fn test_swap_ptr ( ) {
62
63
extern "C" {
63
64
fn swap_ptr ( pptr0 : * mut * const i32 , pptr1 : * mut * const i32 ) ;
64
65
}
65
66
66
- let x = 51 ;
67
- let mut ptr0 = & x;
67
+ let x = 41 ;
68
+ let mut ptr0 = & raw const x;
68
69
let mut ptr1 = std:: ptr:: null ( ) ;
69
70
unsafe { swap_ptr ( & mut ptr0, & mut ptr1) } ;
70
71
71
72
assert_eq ! ( unsafe { * ptr1 } , x) ;
72
73
}
73
74
74
- fn test_init_static_inner ( ) {
75
+ fn test_init_interior_mutable ( ) {
75
76
extern "C" {
76
- fn init_static_inner ( pptr : * const * mut MaybeUninit < i32 > ) ;
77
+ fn init_interior_mutable ( pptr : * const UnsafeInterior ) ;
77
78
}
78
79
79
- static mut INNER : MaybeUninit < i32 > = MaybeUninit :: uninit ( ) ;
80
- static STATIC : * mut MaybeUninit < i32 > = & raw mut INNER ;
81
- unsafe { init_static_inner ( & STATIC ) }
80
+ #[ repr( C ) ]
81
+ struct UnsafeInterior {
82
+ mut_ptr : * mut i32
83
+ }
84
+ unsafe impl Sync for UnsafeInterior { }
85
+
86
+ let mut x = MaybeUninit :: < i32 > :: uninit ( ) ;
87
+ let unsafe_interior = UnsafeInterior { mut_ptr : x. as_mut_ptr ( ) } ;
88
+ unsafe { init_interior_mutable ( & unsafe_interior) } ;
82
89
83
- assert_eq ! ( unsafe { INNER . assume_init( ) } , 61 ) ;
90
+ assert_eq ! ( unsafe { x . assume_init( ) } , 51 ) ;
84
91
}
85
92
86
93
fn test_dangling ( ) {
87
94
extern "C" {
88
- fn write_nullptr ( pptr : * mut * const i32 ) ;
95
+ fn overwrite_ptr ( pptr : * mut * const i32 ) ;
89
96
}
90
97
91
- let x = vec ! [ 71 ] ;
98
+ let x = vec ! [ 61 ] ;
92
99
let mut ptr = x. as_ptr ( ) ;
93
100
drop ( x) ;
94
- unsafe { write_nullptr ( & mut ptr) } ;
101
+ unsafe { overwrite_ptr ( & mut ptr) } ;
95
102
assert_eq ! ( ptr, std:: ptr:: null( ) ) ;
96
103
}
97
104
98
- // TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.
105
+ // TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.
0 commit comments