File tree Expand file tree Collapse file tree 5 files changed +65
-2
lines changed
SwiftRustIntegrationTestRunner
SwiftRustIntegrationTestRunnerTests
SwiftRustIntegrationTestRunner
swift-integration-tests/src Expand file tree Collapse file tree 5 files changed +65
-2
lines changed Original file line number Diff line number Diff line change @@ -10,3 +10,7 @@ import Foundation
10
10
func create_swift_string( ) -> String {
11
11
" hello "
12
12
}
13
+
14
+ func reflect_rust_string( arg: RustString ) -> RustString {
15
+ arg
16
+ }
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ class StringTests: XCTestCase {
40
40
41
41
func testRustStringToString( ) throws {
42
42
let string = " hi "
43
-
43
+
44
44
XCTAssertEqual (
45
45
create_string ( string) . toString ( ) ,
46
46
" hi "
Original file line number Diff line number Diff line change @@ -49,7 +49,13 @@ impl BridgeableType for BridgedString {
49
49
50
50
fn to_swift_type ( & self , type_pos : TypePosition , _types : & TypeDeclarations ) -> String {
51
51
match type_pos {
52
- TypePosition :: FnArg ( _func_host_lang, _) => "GenericIntoRustString" . to_string ( ) ,
52
+ TypePosition :: FnArg ( func_host_lang, _) => {
53
+ if func_host_lang. is_rust ( ) {
54
+ "GenericIntoRustString" . to_string ( )
55
+ } else {
56
+ "UnsafeMutableRawPointer" . to_string ( )
57
+ }
58
+ }
53
59
TypePosition :: FnReturn ( func_host_lang) => {
54
60
if func_host_lang. is_rust ( ) {
55
61
"RustString" . to_string ( )
Original file line number Diff line number Diff line change @@ -262,3 +262,50 @@ func __swift_bridge__some_function () -> UnsafeMutableRawPointer {
262
262
. test ( ) ;
263
263
}
264
264
}
265
+
266
+ /// Test code generation for Swift function that takes and returns an owned String argument.
267
+ mod extern_swift_func_takes_and_returns_string {
268
+ use super :: * ;
269
+
270
+ fn bridge_module_tokens ( ) -> TokenStream {
271
+ quote ! {
272
+ mod foo {
273
+ extern "Swift" {
274
+ fn some_function ( value: String ) -> String ;
275
+ }
276
+ }
277
+ }
278
+ }
279
+
280
+ fn expected_rust_tokens ( ) -> ExpectedRustTokens {
281
+ ExpectedRustTokens :: Contains ( quote ! {
282
+ pub fn some_function ( value: String ) -> String {
283
+ unsafe {
284
+ Box :: from_raw ( unsafe { __swift_bridge__some_function ( swift_bridge :: string :: RustString ( value) . box_into_raw ( ) ) } ) . 0
285
+ }
286
+ }
287
+ } )
288
+ }
289
+
290
+ const EXPECTED_SWIFT_CODE : ExpectedSwiftCode = ExpectedSwiftCode :: ContainsAfterTrim (
291
+ r#"
292
+ @_cdecl("__swift_bridge__$some_function")
293
+ func __swift_bridge__some_function (_ value: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
294
+ { let rustString = some_function(value: RustString(ptr: value)).intoRustString(); rustString.isOwned = false; return rustString.ptr }()
295
+ }
296
+ "# ,
297
+ ) ;
298
+
299
+ const EXPECTED_C_HEADER : ExpectedCHeader = ExpectedCHeader :: ExactAfterTrim ( r#""# ) ;
300
+
301
+ #[ test]
302
+ fn extern_rust_fn_takes_and_returns_string ( ) {
303
+ CodegenTest {
304
+ bridge_module : bridge_module_tokens ( ) . into ( ) ,
305
+ expected_rust_tokens : expected_rust_tokens ( ) ,
306
+ expected_swift_code : EXPECTED_SWIFT_CODE ,
307
+ expected_c_header : EXPECTED_C_HEADER ,
308
+ }
309
+ . test ( ) ;
310
+ }
311
+ }
Original file line number Diff line number Diff line change @@ -8,13 +8,19 @@ mod ffi {
8
8
9
9
extern "Swift" {
10
10
fn create_swift_string ( ) -> String ;
11
+ fn reflect_rust_string ( arg : String ) -> String ;
11
12
}
12
13
}
13
14
14
15
fn run_string_tests ( ) {
15
16
let string = ffi:: create_swift_string ( ) ;
16
17
assert_eq ! ( string. len( ) , 5 ) ;
17
18
assert_eq ! ( & string, "hello" ) ;
19
+
20
+ let foo = "foo" ;
21
+ let string = ffi:: reflect_rust_string ( foo. to_string ( ) ) ;
22
+ assert_eq ! ( string. len( ) , 3 ) ;
23
+ assert_eq ! ( & string, foo) ;
18
24
}
19
25
20
26
fn create_string ( str : & str ) -> String {
You can’t perform that action at this time.
0 commit comments