Skip to content

Commit

Permalink
Improve signature support docs (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn authored Apr 9, 2024
1 parent 7fc3d3c commit 75a1077
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions book/src/contributing/adding-support-for-a-signature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ This chapter shows how to add support for an unsupported signature.

## Implementing Support for a Signature

To support a new signature, we first write automated tests for the signature and then implement just enough code to get those
To support a new signature, we first write automated tests for the signature and then implement just enough code to get
those
tests passing.

Add the time of writing, the `Swift` programming language does not have support for 128 bit integers.
Expand Down Expand Up @@ -71,18 +72,28 @@ would be a good choice.
Before adding our `u128` support, the file looks like this:

```rust
{{#include ../../../../crates/swift-integration-tests/src/primitive.rs::10}}
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
fn test_rust_calls_swift_primitives();

fn rust_double_u8(arg: u8) -> u8;
fn rust_double_i8(arg: i8) -> i8;
fn rust_double_u16(arg: u16) -> u16;
fn rust_double_i16(arg: i16) -> i16;
fn rust_double_u32(arg: u32) -> u32;
// ... snip ...
```

Next we would add our `reflect_u128` function to the bridge module.

We would then modify the `SwiftRustIntegrationTestRunner` to call our function.

In this case we would want to modify [`SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift`](https://github.com/chinedufn/swift-bridge/blob/master/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift),
In this case we would want to
modify [`SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift`](https://github.com/chinedufn/swift-bridge/blob/master/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift),
which before our updates looks something like:

```rust
```swift
import XCTest
@testable import SwiftRustIntegrationTestRunner

Expand Down Expand Up @@ -114,6 +125,7 @@ class PrimitiveTests: XCTestCase {
#### Codegen Tests

After adding one or more integration tests, we would then add one or more codegen tests.
Codegen tests live in `crates/swift-bridge-ir/src/codegen/codegen_tests`.

In codegen tests we write out the exact code that we expect `swift-bridge` to generate.

Expand Down Expand Up @@ -182,7 +194,7 @@ struct __private__OptionF32 __swift_bridge__$some_function(struct __private__Opt
expected_swift_code: expected_swift_code(),
expected_c_header: EXPECTED_C_HEADER,
}
.test();
.test();
}
}
```
Expand Down

0 comments on commit 75a1077

Please sign in to comment.