-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds support for tuples. related to #188. Here's an example of using this: ```rust #[swift_bridge::bridge] mod ffi { extern "Rust" { fn reflect_tuple_primitives(tuple: (i16, u32)) -> (i16, u32); } } fn reflect_tuple_primitives(tuple: (i16, u32)) -> (i16, u32) { tuple } ``` ```swift //... let tuple = reflect_tuple_primitives((-1, 10)) //... ```
- Loading branch information
Showing
24 changed files
with
1,053 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/TupleTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// TupleTests.swift | ||
// SwiftRustIntegrationTestRunnerTests | ||
// | ||
// Created by Niwaka on 2023/03/12. | ||
// | ||
|
||
import Foundation | ||
|
||
import XCTest | ||
@testable import SwiftRustIntegrationTestRunner | ||
|
||
/// Tests tuples | ||
final class TupleTest: XCTestCase { | ||
/// Verify that we can pass and return Rust tuples. | ||
func testSwiftCallsRustTuples() throws { | ||
XCTContext.runActivity(named: "Verify that we can pass and return a (primitive type, primitive type).") { | ||
_ in | ||
let tuple = rust_reflect_tuple_primitives((-1, 10)) | ||
XCTAssertEqual(tuple.0, -1) | ||
XCTAssertEqual(tuple.1, 10) | ||
} | ||
XCTContext.runActivity(named: "Verify that we can pass and return a (OpaqueRustType, String, primitive type).") { | ||
_ in | ||
let tuple = rust_reflect_tuple_opaque_rust_and_string_and_primitive((TupleTestOpaqueRustType(123), "foo", 128)) | ||
XCTAssertEqual(tuple.0.val(), 123) | ||
XCTAssertEqual(tuple.1.toString(), "foo") | ||
XCTAssertEqual(tuple.2, 128) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# (A, B, C, ...) <---> (A, B, C, ...) | ||
|
||
Rust's `(A, B, C, ...)` is seen on the Swift side as a `(A, B, C, ...)`. | ||
|
||
## Returning Tuple from Rust -> Swift | ||
|
||
```rust | ||
// Rust | ||
|
||
mod ffi { | ||
extern "Rust" { | ||
fn get_midpoint( | ||
point1: (f32, f32, f32), | ||
point2: (f32, f32, f32), | ||
) -> (f32, f32, f32); | ||
} | ||
|
||
extern "Swift" { | ||
fn make_point() -> (f32, f32, f32); | ||
} | ||
} | ||
|
||
fn get_midpoint( | ||
point1: (f32, f32, f32), | ||
point2: (f32, f32, f32) | ||
) -> (f32, f32, f32) { | ||
// ... | ||
} | ||
``` | ||
|
||
```swift | ||
// Swift | ||
|
||
func make_point() -> (Float, Float, Float) { | ||
(1.0, 2.0, 3.0) | ||
} | ||
|
||
let midpoint = get_midpoint( | ||
make_point(1.0, 2.0, 3.0), | ||
make_point(4.0, 5.0, 6.0) | ||
) | ||
``` | ||
|
||
## Taking Tuple from Swift -> Rust | ||
|
||
```rust | ||
// Rust | ||
|
||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Rust" { | ||
type SomeRustType; | ||
} | ||
extern "Rust" { | ||
fn run( | ||
arg: (SomeRustType, i32) | ||
); | ||
} | ||
} | ||
``` | ||
|
||
```swift | ||
// Swift | ||
|
||
let someType = SomeType() | ||
run((someType, 123)) | ||
``` |
Oops, something went wrong.