Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple-bridge-modules example #302

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [

"examples/async-functions",
"examples/codegen-visualizer",
"examples/multiple-bridge-modules",
"examples/rust-binary-calls-swift-package",
"examples/without-a-bridge-module",
]
2 changes: 2 additions & 0 deletions examples/multiple-bridge-modules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main
generated
16 changes: 16 additions & 0 deletions examples/multiple-bridge-modules/Cargo.toml
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 = "../../" }
17 changes: 17 additions & 0 deletions examples/multiple-bridge-modules/README.md
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
```
7 changes: 7 additions & 0 deletions examples/multiple-bridge-modules/bridging-header.h
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
13 changes: 13 additions & 0 deletions examples/multiple-bridge-modules/build.rs
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"));
}
10 changes: 10 additions & 0 deletions examples/multiple-bridge-modules/main.swift
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())
16 changes: 16 additions & 0 deletions examples/multiple-bridge-modules/run.sh
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
5 changes: 5 additions & 0 deletions examples/multiple-bridge-modules/src/bridge.rs
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;
19 changes: 19 additions & 0 deletions examples/multiple-bridge-modules/src/bridge/bank.rs
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 }
}
22 changes: 22 additions & 0 deletions examples/multiple-bridge-modules/src/bridge/user.rs
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(),
}
}
1 change: 1 addition & 0 deletions examples/multiple-bridge-modules/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod bridge;
6 changes: 6 additions & 0 deletions examples/without-a-bridge-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ pub struct User {
name: String
}
```

## To Run

```sh
./run.sh
```
Loading