From e7baf0b46ad0079ad036dffc878b377e0f100b45 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sat, 16 Nov 2024 15:23:13 -0500 Subject: [PATCH] Add multiple-bridge-modules example This commit introduces an example of using multiple bridge modules in a project. --- Cargo.toml | 1 + examples/multiple-bridge-modules/.gitignore | 2 ++ examples/multiple-bridge-modules/Cargo.toml | 16 ++++++++++++++ examples/multiple-bridge-modules/README.md | 17 ++++++++++++++ .../multiple-bridge-modules/bridging-header.h | 7 ++++++ examples/multiple-bridge-modules/build.rs | 13 +++++++++++ examples/multiple-bridge-modules/main.swift | 10 +++++++++ examples/multiple-bridge-modules/run.sh | 16 ++++++++++++++ .../multiple-bridge-modules/src/bridge.rs | 5 +++++ .../src/bridge/bank.rs | 19 ++++++++++++++++ .../src/bridge/user.rs | 22 +++++++++++++++++++ examples/multiple-bridge-modules/src/lib.rs | 1 + examples/without-a-bridge-module/README.md | 6 +++++ 13 files changed, 135 insertions(+) create mode 100644 examples/multiple-bridge-modules/.gitignore create mode 100644 examples/multiple-bridge-modules/Cargo.toml create mode 100644 examples/multiple-bridge-modules/README.md create mode 100644 examples/multiple-bridge-modules/bridging-header.h create mode 100644 examples/multiple-bridge-modules/build.rs create mode 100644 examples/multiple-bridge-modules/main.swift create mode 100755 examples/multiple-bridge-modules/run.sh create mode 100644 examples/multiple-bridge-modules/src/bridge.rs create mode 100644 examples/multiple-bridge-modules/src/bridge/bank.rs create mode 100644 examples/multiple-bridge-modules/src/bridge/user.rs create mode 100644 examples/multiple-bridge-modules/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 73e8f0d3..9cc81052 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", ] diff --git a/examples/multiple-bridge-modules/.gitignore b/examples/multiple-bridge-modules/.gitignore new file mode 100644 index 00000000..8186eef2 --- /dev/null +++ b/examples/multiple-bridge-modules/.gitignore @@ -0,0 +1,2 @@ +main +generated diff --git a/examples/multiple-bridge-modules/Cargo.toml b/examples/multiple-bridge-modules/Cargo.toml new file mode 100644 index 00000000..b977f348 --- /dev/null +++ b/examples/multiple-bridge-modules/Cargo.toml @@ -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 = "../../" } \ No newline at end of file diff --git a/examples/multiple-bridge-modules/README.md b/examples/multiple-bridge-modules/README.md new file mode 100644 index 00000000..0e1258ea --- /dev/null +++ b/examples/multiple-bridge-modules/README.md @@ -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 +``` diff --git a/examples/multiple-bridge-modules/bridging-header.h b/examples/multiple-bridge-modules/bridging-header.h new file mode 100644 index 00000000..a68afe01 --- /dev/null +++ b/examples/multiple-bridge-modules/bridging-header.h @@ -0,0 +1,7 @@ +#ifndef BridgingHeader_h +#define BridgingHeader_h + +#include "./generated/SwiftBridgeCore.h" +#include "./generated/multiple-bridge-modules/multiple-bridge-modules.h" + +#endif diff --git a/examples/multiple-bridge-modules/build.rs b/examples/multiple-bridge-modules/build.rs new file mode 100644 index 00000000..b0bcd53e --- /dev/null +++ b/examples/multiple-bridge-modules/build.rs @@ -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")); +} diff --git a/examples/multiple-bridge-modules/main.swift b/examples/multiple-bridge-modules/main.swift new file mode 100644 index 00000000..0af3de40 --- /dev/null +++ b/examples/multiple-bridge-modules/main.swift @@ -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()) \ No newline at end of file diff --git a/examples/multiple-bridge-modules/run.sh b/examples/multiple-bridge-modules/run.sh new file mode 100755 index 00000000..6c994ea4 --- /dev/null +++ b/examples/multiple-bridge-modules/run.sh @@ -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 diff --git a/examples/multiple-bridge-modules/src/bridge.rs b/examples/multiple-bridge-modules/src/bridge.rs new file mode 100644 index 00000000..98f2a68f --- /dev/null +++ b/examples/multiple-bridge-modules/src/bridge.rs @@ -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; diff --git a/examples/multiple-bridge-modules/src/bridge/bank.rs b/examples/multiple-bridge-modules/src/bridge/bank.rs new file mode 100644 index 00000000..90c29f59 --- /dev/null +++ b/examples/multiple-bridge-modules/src/bridge/bank.rs @@ -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 } +} diff --git a/examples/multiple-bridge-modules/src/bridge/user.rs b/examples/multiple-bridge-modules/src/bridge/user.rs new file mode 100644 index 00000000..be2bf4cf --- /dev/null +++ b/examples/multiple-bridge-modules/src/bridge/user.rs @@ -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(), + } +} diff --git a/examples/multiple-bridge-modules/src/lib.rs b/examples/multiple-bridge-modules/src/lib.rs new file mode 100644 index 00000000..8ce8b6e8 --- /dev/null +++ b/examples/multiple-bridge-modules/src/lib.rs @@ -0,0 +1 @@ +mod bridge; diff --git a/examples/without-a-bridge-module/README.md b/examples/without-a-bridge-module/README.md index 2d8bedea..e684c785 100644 --- a/examples/without-a-bridge-module/README.md +++ b/examples/without-a-bridge-module/README.md @@ -16,3 +16,9 @@ pub struct User { name: String } ``` + +## To Run + +```sh +./run.sh +```