@@ -11,16 +11,15 @@ fn main() {
1111 test_static ( ) ;
1212}
1313
14- // Test function that dereferences a pointer and prints its contents from C.
14+ // Test void function that dereferences a pointer and prints its contents from C.
1515fn test_pointer ( ) {
1616 extern "C" {
17- fn print_pointer ( ptr : * mut i32 ) ;
17+ fn print_pointer ( ptr : * const i32 ) ;
1818 }
1919
20- let mut x = 42 ;
21- let ptr = & mut x as * mut i32 ;
20+ let x = 42 ;
2221
23- unsafe { print_pointer ( ptr ) } ;
22+ unsafe { print_pointer ( & x ) } ;
2423}
2524
2625// Test function that dereferences a simple struct pointer and accesses a field.
@@ -31,14 +30,12 @@ fn test_simple() {
3130 }
3231
3332 extern "C" {
34- fn access_simple ( s_ptr : * mut Simple ) -> i32 ;
33+ fn access_simple ( s_ptr : * const Simple ) -> i32 ;
3534 }
3635
37- let mut simple = Simple { field : -42 } ;
38- let s_ptr = & mut simple as * mut Simple ;
36+ let simple = Simple { field : -42 } ;
3937
40- let result = unsafe { access_simple ( s_ptr) } ;
41- assert_eq ! ( result, -42 ) ;
38+ assert_eq ! ( unsafe { access_simple( & simple) } , -42 ) ;
4239}
4340
4441// Test function that dereferences nested struct pointers and accesses fields.
@@ -53,17 +50,14 @@ fn test_nested() {
5350 }
5451
5552 extern "C" {
56- fn access_nested ( n_ptr : * mut Nested ) -> i32 ;
53+ fn access_nested ( n_ptr : * const Nested ) -> i32 ;
5754 }
5855
59- let mut nested_0 = Nested { value : 0 , next : None } ;
60- let mut nested_1 = Nested { value : 1 , next : NonNull :: new ( & mut nested_0) } ;
61- let mut nested_2 = Nested { value : 2 , next : NonNull :: new ( & mut nested_1) } ;
62- let mut nested_3 = Nested { value : 3 , next : NonNull :: new ( & mut nested_2) } ;
63- let n_ptr = & mut nested_3 as * mut Nested ;
56+ let mut nested_0 = Nested { value : 97 , next : None } ;
57+ let mut nested_1 = Nested { value : 98 , next : NonNull :: new ( & mut nested_0) } ;
58+ let nested_2 = Nested { value : 99 , next : NonNull :: new ( & mut nested_1) } ;
6459
65- let result = unsafe { access_nested ( n_ptr) } ;
66- assert_eq ! ( result, 0 ) ;
60+ assert_eq ! ( unsafe { access_nested( & nested_2) } , 97 ) ;
6761}
6862
6963// Test function that dereferences static struct pointers and accesses fields.
@@ -84,6 +78,5 @@ fn test_static() {
8478 recurse : & STATIC ,
8579 } ;
8680
87- let result = unsafe { access_static ( & STATIC ) } ;
88- assert_eq ! ( result, 9001 ) ;
89- }
81+ assert_eq ! ( unsafe { access_static( & STATIC ) } , 9001 ) ;
82+ }
0 commit comments