All Projects → chinedufn → swift-bridge

chinedufn / swift-bridge

Licence: other
swift-bridge facilitates Rust and Swift interop.

Programming Languages

rust
11053 projects
swift
15916 projects

Projects that are alternatives of or similar to swift-bridge

DotNET.jl
Julia ❤️ .NET
Stars: ✭ 75 (-71.15%)
Mutual labels:  ffi, interop
rust-lang-interop
Rust language interoperability with other languages - C, C++ etc.
Stars: ✭ 53 (-79.62%)
Mutual labels:  ffi, interop
clr-loader
Loader for different .NET runtimes
Stars: ✭ 16 (-93.85%)
Mutual labels:  ffi
winsafe
Windows API and GUI in safe, idiomatic Rust.
Stars: ✭ 110 (-57.69%)
Mutual labels:  ffi
idris-ffi-example
A minimal example of the Idris C FFI
Stars: ✭ 14 (-94.62%)
Mutual labels:  ffi
wasm
Utilities for loading and running WASM modules from Dart code
Stars: ✭ 252 (-3.08%)
Mutual labels:  ffi
windigo
Windows API and GUI in idiomatic Go.
Stars: ✭ 187 (-28.08%)
Mutual labels:  ffi
ffi-clang
Ruby FFI bindings for libclang 3.4+.
Stars: ✭ 41 (-84.23%)
Mutual labels:  ffi
eta-ffi
A command line tool to automate the generation of ffi import code for the bindings of various Java libraries.
Stars: ✭ 19 (-92.69%)
Mutual labels:  ffi
pony-sodium
Safe Pony FFI wrapper for the libsodium cryptography library. 🐴 🔐
Stars: ✭ 24 (-90.77%)
Mutual labels:  ffi
php-rdkafka-ffi
PHP Kafka client - binding librdkafka via FFI
Stars: ✭ 49 (-81.15%)
Mutual labels:  ffi
speckle-qgis
QGIS Connector for Speckle 2.0
Stars: ✭ 17 (-93.46%)
Mutual labels:  interop
opencl-ruby
OpenCL bindings for Ruby
Stars: ✭ 38 (-85.38%)
Mutual labels:  ffi
lonlat bng
A multithreaded Rust library with FFI for converting WGS84 longitude and latitude coordinates into BNG (OSGB36) Eastings and Northings and vice versa (using OSTN15)
Stars: ✭ 20 (-92.31%)
Mutual labels:  ffi
DirectN
Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, etc.
Stars: ✭ 128 (-50.77%)
Mutual labels:  interop
pybluemonday
pybluemonday is a library for sanitizing HTML very quickly via bluemonday.
Stars: ✭ 25 (-90.38%)
Mutual labels:  ffi
ol
Otus Lisp (Ol in short) is a purely* functional dialect of Lisp.
Stars: ✭ 157 (-39.62%)
Mutual labels:  ffi
speckle-unity
AEC Interoperability for Unity through Speckle
Stars: ✭ 28 (-89.23%)
Mutual labels:  interop
buke
full text search manpages
Stars: ✭ 27 (-89.62%)
Mutual labels:  ffi
purescript-ffi-utils
A utility library for the purescript foreign function interface
Stars: ✭ 22 (-91.54%)
Mutual labels:  ffi

swift-bridge Actions Status docs

swift-bridge facilitates Rust and Swift interop.

swift-bridge is a library that lets you pass and share high-level types such as Option<T>, String, Struct and Class between Rust and Swift.

You declare the types and functions that you want to import and export using "bridge modules", and then run swift-bridge-build at build time to generate the Swift and C FFI layer to make it all work.

Installation

# In your Cargo.toml

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

[dependencies]
swift-bridge = "0.1"

Book

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

Quick Peek

Here's a quick peek at how you might describe an FFI boundary between Swift and Rust using a bridge module.

// Use the `swift_bridge::bridge` macro to declare a bridge module that
// `swift-bridge-build` will parse at build time in order to generate
// the necessary Swift and C FFI glue code.
#[swift_bridge::bridge]
mod ffi {
    // Create shared structs where both Rust and Swift can directly access the fields.
    struct AppConfig {
        file_manager: CustomFileManager,
    }

    // Shared enums are also supported
    enum UserLookup {
        ById(u32),
        ByName(String),
    }

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

        #[swift_bridge(init)]
        fn new(config: AppConfig);
        
        fn insert_user(&mut self, user_id: u32, user: User);
        fn get_user(&self, lookup: UserLookup) -> Option<&User>;
    }

    extern "Rust" {
        type User;

        #[swift_bridge(init)]
        fn new(user_id: u32, name: String, email: Option<String>) -> User;
    }

    // Import Swift classes and functions for Rust to use.
    extern "Swift" {
        type CustomFileManager;
        fn save_file(&self, name: &str, contents: &[u8]);
    }
}

Quick Start

The swift-bridge repository contains example applications that you use to quickly try out the library, or as a starting point for your own Swift + Rust based application.

For example, here's how to run the ios-rust-analyzer example project locally.

git clone https://github.com/chinedufn/swift-bridge
cd swift-bridge/examples/ios-rust-analyzer

open IosRustAnalyzer/IosRustAnalyzer.xcodeproj
# *** Click the "Run" button at the top left of Xcode ***

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

Built-In Types

In addition to allowing you to share your own custom structs, enums and classes 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<T> RustVec<T>
SwiftArray<T> Array<T> Not yet implemented
&[T] Not yet implemented
&mut [T] Not yet implemented
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 [email protected]:chinedufn/swift-bridge.git
cd swift-bridge

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

Phases

Phase 1 (Current Phase): Make it Possible

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

During this phase we'll focus on adding support for more types, patterns and and common use cases that we discover.

While we must always be thoughtful, we won't be obsessively focused on what the best names, arguments or approaches are during this phase.

Phase 2: Make it Ergonomic

This phase will be focused on making swift-bridge feel really good to use.

During this phase we will:

  • Simplify our APIs and make them consistent.

  • Improve our error messages.

  • Improve the information and examples in the book.

Phase 3: Make it Stable

This phase is about getting swift-bridge to version 1.0.0.

We'll take inventory of all of our public APIs and aim to remove as much we can without impacting the libraries usability.


The 0.1.x versions will not follow semver.

We will maintain semver from 0.2 and onwards.


License

Licensed under MIT or Apache-2.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].