Commit 225380e 1 parent d64db64 commit 225380e Copy full SHA for 225380e
File tree 13 files changed +135
-0
lines changed
13 files changed +135
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ members = [
38
38
39
39
" examples/async-functions" ,
40
40
" examples/codegen-visualizer" ,
41
+ " examples/multiple-bridge-modules" ,
41
42
" examples/rust-binary-calls-swift-package" ,
42
43
" examples/without-a-bridge-module" ,
43
44
]
Original file line number Diff line number Diff line change
1
+ main
2
+ generated
Original file line number Diff line number Diff line change
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 = " ../../" }
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 ( ) )
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ mod bridge;
Original file line number Diff line number Diff line change @@ -16,3 +16,9 @@ pub struct User {
16
16
name : String
17
17
}
18
18
```
19
+
20
+ ## To Run
21
+
22
+ ``` sh
23
+ ./run.sh
24
+ ```
You can’t perform that action at this time.
0 commit comments