21 releases

Uses new Rust 2021

new 0.1.20 Jan 22, 2022
0.1.19 Jan 17, 2022
0.1.15 Dec 28, 2021
0.1.5 Nov 26, 2021

#74 in Procedural macros

Download history 93/week @ 2021-11-20 51/week @ 2021-11-27 54/week @ 2021-12-04 18/week @ 2021-12-11 124/week @ 2021-12-25 62/week @ 2022-01-01 30/week @ 2022-01-08 42/week @ 2022-01-15

258 downloads per month
Used in swift-bridge

Apache-2.0/MIT

370KB
9K SLoC

swift-bridge Actions Status docs

swift-bridge facilitates Rust and Swift interop.

Book

You can find information about using Rust and Swift together in The swift-bridge book.

Quick Peek

Share types and functions between Swift and Rust.

// Rust

// You write the type signatures of your FFI boundary in Rust,
// which `swift-bridge` then uses to generate the FFI layer.
#[swift_bridge::bridge]
mod ffi {
    // Create structs that both Swift and Rust can use.
    #[swift_bridge(swift_repr = "struct")]
    struct Comparison {
        summary: Option<String>,
    }

    // Export Rust types and functions for Swift to use.
    extern "Rust" {
        type ARustStack;

        #[swift_bridge(init)]
        fn new() -> ARustStack;

        fn push(&mut self, val: String);
        fn pop(&mut self) -> Option<String>;
    }

    // Import Swift types and functions for Rust to use.
    extern "Swift" {
        type ASwiftGraph;

        fn compare_graphs(g1: &ASwiftGraph, g2: &ASwiftGraph) -> Comparison;
    }
}

struct ARustStack {
    stack: Vec<String>
}
impl ARustStack {
    // ...
}
// Swift

let stack = ARustStack()
stack.push("Hello, hello.")
let hello = stack.pop()!

class ASwiftGraph {
    // ...
}

func compare_graphs(g1: &ASwiftGraph, g2: &ASwiftGraph) -> Comparison {
    // ...
    return Comparison(summary: "Things went well.")
}

Installation

# In your Cargo.toml

[build-dependencies]
swift-bridge-build = "0.1"

[dependencies]
swift-bridge = "0.1"

Built-In Types

In addition to allowing you to share your own custom types between Rust and Swift, 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, RustStringRef, RustStringRefMut
&str RustStr
Vec RustVec<T>
SwiftArray<T> Array<T> Not yet implemented
&[T] UnsafeBufferPointer<T>
&mut [T] UnsafeMutableBufferPointer<T> Not yet implemented
SwiftString String
Box Not yet implemented
[T; N] Not yet implemented
*const T UnsafePointer<T>
*mut T UnsafeMutablePointer<T>
Option<T> Optional<T>
Result<T> Not yet implemented
Have a Rust standard library type in mind?
Open an issue!
Have a Swift standard library type in mind?
Open an issue!

To Test

To run the test suite.

# Clone the repository
git clone git@github.com:chinedufn/swift-bridge.git
cd swift-bridge

# Run tests
cargo test --all && ./test-integration.sh

Early Stages

Bridging Rust and Swift is fairly unexplored territory, so it will take some experimentation in order to figure out the right API and code generation.

In these early days I'm looking for feedback from bleeding-edge users in order to continue to improve the API and the generated code.

I can especially use feedback from people with Swift experience, since I don't have much.


The 0.1.x versions will not follow semver.

We'll maintain semver from 0.2 and onwards.

See Also

  • Rust on iOS - A blog post by Mozilla that explains how to run Rust on iOS.

  • cxx - swift-bridge takes inspiration from the bridge module idea pioneered by cxx.

Dependencies

~280–700KB
~17K SLoC

`a