1
1
use crate :: bridged_type:: { SharedStruct , StructSwiftRepr } ;
2
- use proc_macro2:: { TokenStream } ;
2
+ use proc_macro2:: TokenStream ;
3
3
use quote:: quote;
4
4
5
-
6
5
/// Generate the functions that Swift calls uses inside of the corresponding class for a
7
6
/// transparent struct's Vectorizable implementation.
8
7
///
9
8
/// So inside of `extension SomeTransparentStruct: Vectorizable {}` on the Swift side.
10
9
pub ( in super :: super ) fn generate_vec_of_transparent_struct_functions (
11
- shared_struct : & SharedStruct
10
+ shared_struct : & SharedStruct ,
12
11
) -> TokenStream {
13
12
if can_generate_vec_of_transparent_struct_functions ( & shared_struct) {
14
13
let struct_name = & shared_struct. name ;
15
-
14
+
16
15
// examples:
17
16
// "__swift_bridge__$Vec_SomeTransparentStruct$new"
18
17
// "__swift_bridge__$Vec_SomeTransparentStruct$drop"
@@ -31,7 +30,7 @@ pub(in super::super) fn generate_vec_of_transparent_struct_functions(
31
30
let export_name_push = make_export_name ( "push" ) ;
32
31
let export_name_pop = make_export_name ( "pop" ) ;
33
32
let export_name_as_ptr = make_export_name ( "as_ptr" ) ;
34
-
33
+
35
34
let ffi_struct_repr = & shared_struct. ffi_name_tokens ( ) ;
36
35
let ffi_option_struct_repr = shared_struct. ffi_option_name_tokens ( ) ;
37
36
// TODO: Check for trait implementation as well
@@ -40,58 +39,58 @@ pub(in super::super) fn generate_vec_of_transparent_struct_functions(
40
39
} else {
41
40
quote ! { v. clone( ) }
42
41
} ;
43
-
42
+
44
43
quote ! {
45
44
const _: ( ) = {
46
45
#[ doc( hidden) ]
47
46
#[ export_name = #export_name_new]
48
47
pub extern "C" fn _new( ) -> * mut Vec <#struct_name> {
49
48
Box :: into_raw( Box :: new( Vec :: new( ) ) )
50
49
}
51
-
50
+
52
51
#[ doc( hidden) ]
53
52
#[ export_name = #export_name_drop]
54
53
pub extern "C" fn _drop( vec: * mut Vec <#struct_name>) {
55
54
let vec = unsafe { Box :: from_raw( vec) } ;
56
55
drop( vec)
57
56
}
58
-
57
+
59
58
#[ doc( hidden) ]
60
59
#[ export_name = #export_name_len]
61
60
pub extern "C" fn _len( vec: * const Vec <#struct_name>) -> usize {
62
61
unsafe { & * vec } . len( )
63
62
}
64
-
63
+
65
64
#[ doc( hidden) ]
66
65
#[ export_name = #export_name_get]
67
66
pub extern "C" fn _get( vec: * const Vec <#struct_name>, index: usize ) -> #ffi_option_struct_repr {
68
67
let vec = unsafe { & * vec } ;
69
68
let val = vec. get( index) . map( |v|#vec_map) ;
70
69
#ffi_option_struct_repr:: from_rust_repr( val)
71
70
}
72
-
71
+
73
72
#[ doc( hidden) ]
74
73
#[ export_name = #export_name_get_mut]
75
74
pub extern "C" fn _get_mut( vec: * mut Vec <#struct_name>, index: usize ) -> #ffi_option_struct_repr {
76
75
let vec = unsafe { & mut * vec } ;
77
76
let val = vec. get_mut( index) . map( |v|#vec_map) ;
78
77
#ffi_option_struct_repr:: from_rust_repr( val)
79
78
}
80
-
79
+
81
80
#[ doc( hidden) ]
82
81
#[ export_name = #export_name_push]
83
82
pub extern "C" fn _push( vec: * mut Vec <#struct_name>, val: #ffi_struct_repr) {
84
83
unsafe { & mut * vec } . push( val. into_rust_repr( ) )
85
84
}
86
-
85
+
87
86
#[ doc( hidden) ]
88
87
#[ export_name = #export_name_pop]
89
88
pub extern "C" fn _pop( vec: * mut Vec <#struct_name>) -> #ffi_option_struct_repr {
90
89
let vec = unsafe { & mut * vec } ;
91
90
let val = vec. pop( ) ;
92
91
#ffi_option_struct_repr:: from_rust_repr( val)
93
92
}
94
-
93
+
95
94
#[ doc( hidden) ]
96
95
#[ export_name = #export_name_as_ptr]
97
96
pub extern "C" fn _as_ptr( vec: * const Vec <#struct_name>) -> * const #struct_name {
@@ -104,7 +103,9 @@ pub(in super::super) fn generate_vec_of_transparent_struct_functions(
104
103
}
105
104
}
106
105
107
- pub ( crate ) fn can_generate_vec_of_transparent_struct_functions ( shared_struct : & SharedStruct ) -> bool {
106
+ pub ( crate ) fn can_generate_vec_of_transparent_struct_functions (
107
+ shared_struct : & SharedStruct ,
108
+ ) -> bool {
108
109
match shared_struct. swift_repr {
109
110
StructSwiftRepr :: Class => false ,
110
111
// TODO: Check for trait implementation as well
@@ -115,9 +116,9 @@ pub(crate) fn can_generate_vec_of_transparent_struct_functions(shared_struct: &S
115
116
#[ cfg( test) ]
116
117
mod tests {
117
118
use super :: * ;
119
+ use crate :: bridged_type:: { shared_struct:: StructDerives , StructFields , StructSwiftRepr } ;
118
120
use crate :: test_utils:: assert_tokens_eq;
119
121
use proc_macro2:: { Ident , Span } ;
120
- use crate :: bridged_type:: { StructSwiftRepr , StructFields , shared_struct:: StructDerives } ;
121
122
122
123
/// Verify that we can generate the functions for an opaque Rust type that get exposed to Swift
123
124
/// in order to power the `extension MyRustType: Vectorizable { }` implementation on the Swift
@@ -189,7 +190,10 @@ mod tests {
189
190
fields : StructFields :: Named ( vec ! [ ] ) ,
190
191
swift_name : None ,
191
192
already_declared : false ,
192
- derives : StructDerives { copy : true , clone : true } ,
193
+ derives : StructDerives {
194
+ copy : true ,
195
+ clone : true ,
196
+ } ,
193
197
} ;
194
198
assert_tokens_eq (
195
199
& generate_vec_of_transparent_struct_functions ( & shared_struct) ,
@@ -267,7 +271,10 @@ mod tests {
267
271
fields : StructFields :: Named ( vec ! [ ] ) ,
268
272
swift_name : None ,
269
273
already_declared : false ,
270
- derives : StructDerives { copy : false , clone : true } ,
274
+ derives : StructDerives {
275
+ copy : false ,
276
+ clone : true ,
277
+ } ,
271
278
} ;
272
279
assert_tokens_eq (
273
280
& generate_vec_of_transparent_struct_functions ( & shared_struct) ,
0 commit comments