-
Notifications
You must be signed in to change notification settings - Fork 63
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 suppoer for passing a `Result<T, E>` as an argument from Rust -> Swift. For example, the following is not possible: ```rust use some_crate::SomeRustType; #[swift_bridge::bridge] mod ffi { extern "Swift" { fn swift_func_takes_callback( arg: Box<dyn FnOnce(Result<SomeRustType, String>)>, ); } extern "Rust" { type SomeRustType; #[swift_bridge::init] fn new () -> SomeRustType; } } ```
- Loading branch information
Showing
27 changed files
with
792 additions
and
54 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
12 changes: 12 additions & 0 deletions
12
SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Result.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,12 @@ | ||
// | ||
// Result.swift | ||
// SwiftRustIntegrationTestRunner | ||
// | ||
// Created by Frankie Nwafili on 9/20/22. | ||
// | ||
|
||
func swift_func_takes_callback_with_result_arg( | ||
arg: (RustResult<CallbackTestOpaqueRustType, String>) -> () | ||
) { | ||
arg(.Ok(CallbackTestOpaqueRustType(555))) | ||
} |
27 changes: 27 additions & 0 deletions
27
SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.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,27 @@ | ||
// | ||
// ResultTests.swift | ||
// SwiftRustIntegrationTestRunnerTests | ||
// | ||
// Created by Frankie Nwafili on 9/20/22. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftRustIntegrationTestRunner | ||
|
||
class ResultTests: XCTestCase { | ||
/// Verify that we can pass a Result<String, String> from Swift -> Rust | ||
func testSwiftCallRustResultString() throws { | ||
rust_func_takes_result_string(.Ok("Success Message")) | ||
rust_func_takes_result_string(.Err("Error Message")) | ||
} | ||
|
||
/// Verify that we can pass a Result<OpaqueRust, OpaqueRust> from Swift -> Rust | ||
func testSwiftCallRustResultOpaqueRust() throws { | ||
rust_func_takes_result_opaque_rust( | ||
.Ok(ResultTestOpaqueRustType(111)) | ||
) | ||
rust_func_takes_result_opaque_rust( | ||
.Err(ResultTestOpaqueRustType(222)) | ||
) | ||
} | ||
} |
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,27 @@ | ||
# Result | ||
|
||
Rust's `Result` is seen on the Swift side as a `RustResult`. | ||
|
||
## Example | ||
|
||
```rust,no_run | ||
// Rust | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Swift" { | ||
fn run( | ||
arg: Box<dyn FnOnce(Result<SomeRustType, String>)> | ||
); | ||
} | ||
extern "Rust" { | ||
type SomeRustType; | ||
} | ||
``` | ||
|
||
```swift | ||
func run(arg: (RustResult<SomeRustType, String>) -> ()) { | ||
arg(.Err("Something went wrong")) | ||
} | ||
``` |
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
40 changes: 40 additions & 0 deletions
40
crates/swift-bridge-build/src/generate_core/result_support.rs
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,40 @@ | ||
pub const SWIFT_RUST_RESULT: &'static str = r#" | ||
public enum RustResult<T, E> { | ||
case Ok(T) | ||
case Err(E) | ||
} | ||
extension RustResult { | ||
func ok() -> T? { | ||
switch self { | ||
case .Ok(let ok): | ||
return ok | ||
case .Err(_): | ||
return nil | ||
} | ||
} | ||
func err() -> E? { | ||
switch self { | ||
case .Ok(_): | ||
return nil | ||
case .Err(let err): | ||
return err | ||
} | ||
} | ||
func toResult() -> Result<T, E> | ||
where E: Error { | ||
switch self { | ||
case .Ok(let ok): | ||
return .success(ok) | ||
case .Err(let err): | ||
return .failure(err) | ||
} | ||
} | ||
} | ||
"#; | ||
|
||
pub const C_RESULT_SUPPORT: &'static str = r#" | ||
struct __private__ResultPtrAndPtr { bool is_ok; void* ok_or_err; }; | ||
"#; |
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
Oops, something went wrong.