Skip to content

Commit 225380e

Browse files
authored
Add multiple-bridge-modules example (chinedufn#302)
This commit introduces an example of using multiple bridge modules in a project.
1 parent d64db64 commit 225380e

File tree

13 files changed

+135
-0
lines changed

13 files changed

+135
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ members = [
3838

3939
"examples/async-functions",
4040
"examples/codegen-visualizer",
41+
"examples/multiple-bridge-modules",
4142
"examples/rust-binary-calls-swift-package",
4243
"examples/without-a-bridge-module",
4344
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main
2+
generated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "multiple-bridge-modules"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = []
6+
7+
build = "build.rs"
8+
9+
[lib]
10+
crate-type = ["staticlib"]
11+
12+
[build-dependencies]
13+
swift-bridge-build = { path = "../../crates/swift-bridge-build" }
14+
15+
[dependencies]
16+
swift-bridge = { path = "../../" }
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Multiple Bridge Modules
2+
3+
`swift-bridge` supports defining multiple bridge modules across one or more files.
4+
5+
This example demonstrates how to define and generate code for multiple bridge modules.
6+
7+
The Rust crate contains a `crate::bridge::user` and a `crate::bridge::bank` module.
8+
9+
Each module contains a bridge module that exposes types to `Swift`.
10+
11+
The `main.swift` function uses these bridged types to create and print some information.
12+
13+
## To Run
14+
15+
```sh
16+
./run.sh
17+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef BridgingHeader_h
2+
#define BridgingHeader_h
3+
4+
#include "./generated/SwiftBridgeCore.h"
5+
#include "./generated/multiple-bridge-modules/multiple-bridge-modules.h"
6+
7+
#endif
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::path::PathBuf;
2+
3+
fn main() {
4+
let out_dir = PathBuf::from("./generated");
5+
6+
let bridges = vec!["src/bridge/bank.rs", "src/bridge/user.rs"];
7+
for path in &bridges {
8+
println!("cargo:rerun-if-changed={}", path);
9+
}
10+
11+
swift_bridge_build::parse_bridges(bridges)
12+
.write_all_concatenated(out_dir, env!("CARGO_PKG_NAME"));
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func print_bank_account_amount(bank_account: BankAccount) {
2+
print("Bank Account contains $\(bank_account.amount())")
3+
}
4+
5+
func print_user_name(user: User) {
6+
print("User Name \(user.name().toString())")
7+
}
8+
9+
print_bank_account_amount(bank_account: make_bank_account())
10+
print_user_name(user: make_user())
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
THISDIR=$(dirname $0)
6+
cd $THISDIR
7+
8+
cargo build -p multiple-bridge-modules
9+
10+
swiftc -L ../../target/debug \
11+
-lmultiple_bridge_modules \
12+
-import-objc-header bridging-header.h \
13+
-framework CoreFoundation -framework SystemConfiguration \
14+
main.swift ./generated/SwiftBridgeCore.swift ./generated/multiple-bridge-modules/multiple-bridge-modules.swift
15+
16+
./main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! The `build.rs` script parses both of the child modules and generates the corresponding
2+
//! Swift and C code.
3+
4+
pub(crate) mod bank;
5+
pub(crate) mod user;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[swift_bridge::bridge]
2+
mod ffi {
3+
extern "Rust" {
4+
type BankAccount;
5+
6+
#[swift_bridge(get(amount))]
7+
fn amount(&self) -> u32;
8+
9+
fn make_bank_account() -> BankAccount;
10+
}
11+
}
12+
13+
pub(crate) struct BankAccount {
14+
pub amount: u32,
15+
}
16+
17+
fn make_bank_account() -> BankAccount {
18+
BankAccount { amount: 500 }
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[swift_bridge::bridge]
2+
mod ffi {
3+
extern "Rust" {
4+
type User;
5+
6+
#[swift_bridge(get(&name))]
7+
fn name(&self) -> &str;
8+
9+
fn make_user() -> User;
10+
}
11+
}
12+
13+
struct User {
14+
#[allow(dead_code)]
15+
name: String,
16+
}
17+
18+
fn make_user() -> User {
19+
User {
20+
name: "Bob".to_string(),
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod bridge;

examples/without-a-bridge-module/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ pub struct User {
1616
name: String
1717
}
1818
```
19+
20+
## To Run
21+
22+
```sh
23+
./run.sh
24+
```

0 commit comments

Comments
 (0)