swift-bridge
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 awesome bridge module macro based approach pioneered by cxx.
Installation
# In your Cargo.toml
[]
= "0.1"
[]
= "0.1"
Phases
-
Put the library out to get feedback from bleeding edge users.
-
Continue to support more standard library types.
-
Continue to hone the API based on real usage feedback.
-
Figure out the safety story. What should be marked as safe vs. unsafe?
-
Focus on making usage of swift-bridge feel ergonomic.
-
Polish the documentation, examples and tutorials
-
Get to a point where we feel that there has been enough, real world production use and feedback for us to be confident that the user-facing API's won't need any breaking changes.
-
Release swift-bridge version 1.0
Quick Peek
TODO: Shorten this quick peak.. we don't need to include the build script, for example
Here's a quick peek at the Rust and Swift of a bridge that should give you a sense of how bindings look.
A more thorough walk through of swift-bridge
can be found in the book (TODO: Link to GitHub pages).
// build.rs
// lib.rs
;
// Swift
class SwiftApiClient {
var timeout: UInt8
init(timeout: UInt8) {
self.timeout = timeout
}
class func version() -> u32 {
1
}
func setTimeout(timeout: UInt8) {
self.timeout = timeout
}
}
TODO's before open sourcing
We don't need to solve all of these, but we should at least create issues
-
Look up how to programatically set the linking settings and programatically set the run script. Our docs can recommend that as well as show how to manually set them
-
Remove
#[no_mangle]
since we're using link_name and export_name
Quick Peek
-
Write instructions on going from 0 to most basic iOS app
-
Add book chapter on setting up iOS app from scratch
-
Create examples dir example of iOS app
-
Create examples dir example of macOS app
Built-In Types
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 | 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> | Currently only supported for primitive function return types. Other places such as function arguments are not yet implemented. Non primitive T is not yet implemented. |
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
# Run tests
See Also
- Rust on iOS
- A blog post by Mozilla that explains how to run Rust on iOS.