Call Rust from Swift and vice versa.
swift-bridge
generates code that helps you call Swift from Rust and vice versa.
swift-bridge
takes inspiration from the bridge module idea pioneered by cxx.
swift-bridge
is not yet production ready.
We need to use it more before we can be confident enough in the generated FFI glue to call swift-bridge
production ready.
Right now I'm looking for feedback in order to continue to improve the APIs and the generated code.
I can especially use feedback from people with Swift experience, since I don't have much.
```toml
[build-dependencies] swift-bridge-build = "0.1"
[dependencies] swift-bridge = "0.1" ```
Here's a quick peek at how bindings look.
A more thorough walk through of swift-bridge
can be found in the book (TODO: Link to GitHub pages).
```rust // lib.rs
mod ffi { // Shared types #[swiftbridge(swiftrepr = "struct")] struct ASharedStruct { field: u32 }
// Exposes super::ARustStack to Swift.
extern "Rust" {
type ARustStack;
fn push (&mut self, val: u8);
fn pop (&mut self) -> Option<u8>;
fn as_slice (&self) -> &[u8];
fn do_stuff(override: Option<u8>);
}
// Exposes a Swift class to Rust.
extern "Swift" {
type SwiftApiClient;
#[swift_bridge(init)]
fn new_with_timeout(timeout: u8) -> SwiftApiClient;
#[swift_bridge(associated_to = FileSystemClient)]
fn version () -> String;
fn post_bytes(&self, bytes: &[u8]);
}
} ```
swift_bridge
comes with support for a number of Rust and Swift standard library types.
| name in Rust | name in Swift | notes |
| --- | --- | --- |
| u8, i8, u16, i16... etc | UInt8, Int8, UInt16, Int16 ... etc | |
| bool | Bool | |
| String, &String, &mut String | RustString | |
| &str | RustStr | |
| Vec-> Option<i32>
), or -> Option<String>
..
More support will come.
Other places such as function arguments are not yet implemented but will come.
|
| Result\
Open an issue! | | |
| | Have a Swift standard library type in mind?
Open an issue! | |
To run the test suite.
```sh
git clone git@github.com:chinedufn/swift-bridge.git cd swift-bridge
cargo test --all && ./test-integration.sh ```