-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiple-bridge-modules example (#302)
This commit introduces an example of using multiple bridge modules in a project.
- Loading branch information
Showing
13 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
main | ||
generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "multiple-bridge-modules" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = [] | ||
|
||
build = "build.rs" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[build-dependencies] | ||
swift-bridge-build = { path = "../../crates/swift-bridge-build" } | ||
|
||
[dependencies] | ||
swift-bridge = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Multiple Bridge Modules | ||
|
||
`swift-bridge` supports defining multiple bridge modules across one or more files. | ||
|
||
This example demonstrates how to define and generate code for multiple bridge modules. | ||
|
||
The Rust crate contains a `crate::bridge::user` and a `crate::bridge::bank` module. | ||
|
||
Each module contains a bridge module that exposes types to `Swift`. | ||
|
||
The `main.swift` function uses these bridged types to create and print some information. | ||
|
||
## To Run | ||
|
||
```sh | ||
./run.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef BridgingHeader_h | ||
#define BridgingHeader_h | ||
|
||
#include "./generated/SwiftBridgeCore.h" | ||
#include "./generated/multiple-bridge-modules/multiple-bridge-modules.h" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let out_dir = PathBuf::from("./generated"); | ||
|
||
let bridges = vec!["src/bridge/bank.rs", "src/bridge/user.rs"]; | ||
for path in &bridges { | ||
println!("cargo:rerun-if-changed={}", path); | ||
} | ||
|
||
swift_bridge_build::parse_bridges(bridges) | ||
.write_all_concatenated(out_dir, env!("CARGO_PKG_NAME")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
func print_bank_account_amount(bank_account: BankAccount) { | ||
print("Bank Account contains $\(bank_account.amount())") | ||
} | ||
|
||
func print_user_name(user: User) { | ||
print("User Name \(user.name().toString())") | ||
} | ||
|
||
print_bank_account_amount(bank_account: make_bank_account()) | ||
print_user_name(user: make_user()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
THISDIR=$(dirname $0) | ||
cd $THISDIR | ||
|
||
cargo build -p multiple-bridge-modules | ||
|
||
swiftc -L ../../target/debug \ | ||
-lmultiple_bridge_modules \ | ||
-import-objc-header bridging-header.h \ | ||
-framework CoreFoundation -framework SystemConfiguration \ | ||
main.swift ./generated/SwiftBridgeCore.swift ./generated/multiple-bridge-modules/multiple-bridge-modules.swift | ||
|
||
./main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! The `build.rs` script parses both of the child modules and generates the corresponding | ||
//! Swift and C code. | ||
|
||
pub(crate) mod bank; | ||
pub(crate) mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Rust" { | ||
type BankAccount; | ||
|
||
#[swift_bridge(get(amount))] | ||
fn amount(&self) -> u32; | ||
|
||
fn make_bank_account() -> BankAccount; | ||
} | ||
} | ||
|
||
pub(crate) struct BankAccount { | ||
pub amount: u32, | ||
} | ||
|
||
fn make_bank_account() -> BankAccount { | ||
BankAccount { amount: 500 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Rust" { | ||
type User; | ||
|
||
#[swift_bridge(get(&name))] | ||
fn name(&self) -> &str; | ||
|
||
fn make_user() -> User; | ||
} | ||
} | ||
|
||
struct User { | ||
#[allow(dead_code)] | ||
name: String, | ||
} | ||
|
||
fn make_user() -> User { | ||
User { | ||
name: "Bob".to_string(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod bridge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,9 @@ pub struct User { | |
name: String | ||
} | ||
``` | ||
|
||
## To Run | ||
|
||
```sh | ||
./run.sh | ||
``` |