1
+ use objc2_encode:: { Encode , Encoding } ;
2
+
1
3
/// A block that takes no arguments and returns an integer: `int32_t (^)()`.
2
4
#[ repr( C ) ]
3
5
pub struct IntBlock {
@@ -11,6 +13,37 @@ pub struct AddBlock {
11
13
_priv : [ u8 ; 0 ] ,
12
14
}
13
15
16
+ #[ repr( C ) ]
17
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
18
+ pub struct LargeStruct {
19
+ pub x : f32 ,
20
+ pub y : [ u8 ; 100 ] ,
21
+ }
22
+
23
+ impl LargeStruct {
24
+ pub fn get ( ) -> Self {
25
+ let mut y = [ 10 ; 100 ] ;
26
+ y[ 42 ] = 123 ;
27
+ Self { x : 5.0 , y }
28
+ }
29
+
30
+ pub fn mutate ( & mut self ) {
31
+ self . x -= 1.0 ;
32
+ self . y [ 12 ] += 1 ;
33
+ self . y [ 99 ] = 123 ;
34
+ }
35
+ }
36
+
37
+ unsafe impl Encode for LargeStruct {
38
+ const ENCODING : Encoding < ' static > =
39
+ Encoding :: Struct ( "LargeStruct" , & [ f32:: ENCODING , <[ u8 ; 100 ] >:: ENCODING ] ) ;
40
+ }
41
+
42
+ #[ repr( C ) ]
43
+ pub struct LargeStructBlock {
44
+ _priv : [ u8 ; 0 ] ,
45
+ }
46
+
14
47
extern "C" {
15
48
/// Returns a pointer to a global `IntBlock` that returns 7.
16
49
pub fn get_int_block ( ) -> * mut IntBlock ;
@@ -24,6 +57,10 @@ extern "C" {
24
57
pub fn invoke_int_block ( block : * mut IntBlock ) -> i32 ;
25
58
/// Invokes an `AddBlock` with `a` and returns the result.
26
59
pub fn invoke_add_block ( block : * mut AddBlock , a : i32 ) -> i32 ;
60
+
61
+ pub fn get_large_struct_block ( ) -> * mut LargeStructBlock ;
62
+ pub fn get_large_struct_block_with ( i : LargeStruct ) -> * mut LargeStructBlock ;
63
+ pub fn invoke_large_struct_block ( block : * mut LargeStructBlock , s : LargeStruct ) -> LargeStruct ;
27
64
}
28
65
29
66
#[ cfg( test) ]
@@ -45,4 +82,20 @@ mod tests {
45
82
assert_eq ! ( invoke_add_block( get_add_block_with( 3 ) , 5 ) , 8 ) ;
46
83
}
47
84
}
85
+
86
+ #[ test]
87
+ fn test_large_struct_block ( ) {
88
+ let data = LargeStruct :: get ( ) ;
89
+ let mut expected = data. clone ( ) ;
90
+ expected. mutate ( ) ;
91
+
92
+ assert_eq ! (
93
+ unsafe { invoke_large_struct_block( get_large_struct_block( ) , data) } ,
94
+ expected
95
+ ) ;
96
+ assert_eq ! (
97
+ unsafe { invoke_large_struct_block( get_large_struct_block_with( expected) , data) } ,
98
+ expected
99
+ ) ;
100
+ }
48
101
}
0 commit comments